jQuery, JSON and Apache problem

后端 未结 2 1966

I have an jQuery JSON request, that loads some JSON from another server (ex. foo.com):

$.getJSON(\"http://foo.com/json.php\",function(data) { alert(data); });


        
相关标签:
2条回答
  • 2021-01-21 03:34

    I'm pretty sure that in order to do cross domain calls like this you have to have a callback, it's what's needed to do JSONP.

    here is some more info on jsonp http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html

    For jsonp to work you have to have a callback for the server to wrap the json string in. for example:

    $.getJSON("http://foo.com/json.php?callback=?", function(data){});
    

    here a callback function is generated by jquery and passed into the request, so it would be something like:

    http://foo.com/json.php?callback=generatedFunction
    

    then what's returned by the server should be:

    generatedFunction("{key:value, key2:value2}");
    

    where the parameter in that function is the actual json string.

    in the php to return this it would be something like:

    $callback = $_GET['callback'];
    print($callback."(".json_encode($theobject).");");
    
    0 讨论(0)
  • 2021-01-21 03:35

    More about cross-domain JSON throw JSONP and jQuery.

    0 讨论(0)
提交回复
热议问题