Javascript get output from separate php script

后端 未结 2 1919
礼貌的吻别
礼貌的吻别 2021-01-06 02:04

I want javascript to be able to call a php script (which just echos a string) using jQuery.

I think $.get is the right way, but not too sure.

I

2条回答
  •  执笔经年
    2021-01-06 02:34

    $.get() is the way to go, indeed.

    First of all, you will need a page / url that outputs the result of the function you want to use (for example, *www.yoursite.com/test_output.php* ). You should create that page and call the function you want to use there. Also keep in mind that I said output, not return, because .get will fetch the output of the http response, not the returned value of the php function.

    So if you have the following function defined in your site (you can also define it in test_output.php, of course):

    
    

    In test_output.php, you will need something like this:

    
    

    Then on the client side, you need some JavaScript / jQuery:

    var data_from_ajax;
    
    $.get('http://www.yoursite.com/test_output.php', function(data) {
      data_from_ajax = data;
    });
    

    Now you have the output of the ajax .get() function stored in data_from_ajax and you can use it as you please.

提交回复
热议问题