jQuery getJSON to external PHP page

前端 未结 3 1129
花落未央
花落未央 2020-12-14 12:32

I\'ve been trying to make an AJAX request to an external server. I\'ve learned so far that I need to use getJSON to do this because of security reasons ?

Now, I can\

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 13:16

    It could be that you haven't got a callback in test.php. Also, json_encode only accepts an array:

    $results = array("key" => "value");
    echo $_GET['callback'] . '(' . json_encode($results) . ')';
    // the callback stuff is only needed if you're requesting from different domains
    

    jQuery automatically switches to JSONP (i.e. using script tags instead of XMLHttpRequest) when you use http://. If you have test.html and test.php on the same domain, try using relative paths (and no callbacks).

提交回复
热议问题