Local virtual two domain names, respectively: www.test.com, www.abc.com
Http://www.test.com/index.html page Click button, request to return the data of the file under the www.abc.com domain name directory , the code is:
<! DOCTYPE html>
The http://www.abc.com/json.php file code is:
<?php $arr = Array (' username ' = ' jack ', ' age ' =>21, ' gender ' = ' male '); echo Json_encode ($arr);? >
Debugging in the Chrome browser will find an error, The data returned by json.php is indeed a JSON-type data {"username": "Jack", "Age": $, "gender": "Male"}, where is the problem?
Look at it. jquery Document Discovery Jsonp: "Callback", Jsonpcallback: "Success_jsonpcallback", pass these two parameters for a reason, JSONP the return data format should be: " The client passes the callback method name (JSON data) and changes the PHP file to:
<?php $arr = Array (' username ' = ' jack ', ' age ' =>21, ' gender ' = ' male '); Echo $_get[' callback ']. " (". Json_encode ($arr).") ";? >
Test, return the results correctly, such as:
As you can see, the result of the PHP file return is Success_jsonpcallback ({"username": "Jack", "age": +, "gender": "Male"}), which is the correct JSONP return format, and Success_jsonpcallback This is the pass-through parameter .
JSONP cross-domain Request data Error "unexpected token:" Solution