/* Data can be requested across domains */
The principle is to use a global function (callback) to operate the server data loaded by <script>.
First, write a simple example: (jsonpCallback can dynamically create and register as a global function)
<Script type = "text/javascript">
Var m = Math. random ();
Var jsonpCallback = new Function ("result", "alert (result. data );");
</Script>
<Script type = "text/javascript" src = "http: // localhost/json. php? Jsonp = jsonpCallback "> </script>
Server:
<? Php
Echo "jsonpCallback ({data: 'json data '})";
Json data
$. Get (url, params, callback)
Like the $. post request method, the request type is different.
Returns the character format. You can use the $. evalJSON () method to convert the format.
Then perform operations on the JSON object
$. GetJSON (url, params, callback)
Return a JSON object. The cross-origin example is as follows:
<? Php
Function getdata ()
{
$. GetJSON (
"Http://www.test.com/payment/payment/paytest? Callback =? ",
{Id: 1, name: "name "},
Function (jsondata ){
Alert (jsondata. json );
});
}
Open the source code of jquery and find it step by step:
GetJSON: function (url, data, callback ){
Return jquery. Get (URL, Data, callback, "JSON ");
}
Find jquery. Get again
Get: function (URL, Data, callback, type ){
// Shift arguments if data argument was omited
If (jquery. isfunction (data )){
Type = type | callback;
Callback = data;
Data = NULL;
}
Return jQuery. ajax ({
Type: "GET ",
Url: url,
Data: data,
Success: callback,
DataType: type
});
}
Find jQuery. ajax again
JQuery. ajax ({
Url: url,
Type: type,
DataType: "html ",
Data: params,
Complete: function (res, status ){
// If successful, inject the HTML into all the matched elements
If (status = "success" | status = "notmodified "){
// See if a selector was specified
Self.html (selector?
// Create a dummy div to hold the results
JQuery ("<div/> ")
// Inject the contents of the document in, removing the scripts
// To avoid any 'permission Denied 'errors in IE
. Append (res. responseText. replace (rscript ,""))
// Locate the specified elements
. Find (selector ):
// If not, just inject the full result
Res. responsetext );
}
If (callback ){
Self. each (callback, [res. responseText, status, res]);
}
}
});
Return this;
}
Find the ajax method and uncover the secret:
Because there are too many posts at the beginning, you can check them if you are interested.
Ajax: function (origSettings ){
Var s = jQuery. extend (true, {}, jQuery. ajaxSettings, origSettings );
Var jsonp, status, data,
CallbackContext = origSettings & origSettings. context | s,
Type = s. type. toUpperCase ();
// Convert data if not already a string
If (s. data & s. processData & typeof s. data! = "String "){
S. data = jQuery. param (s. data, s. traditional );
}
Important part:
Http: // localhost/index/ajax? Callback = jsonp00004438515229 & id = 1
The server determines whether the callback parameter exists. If yes, the system returns the JS function name + object.
// Jsonp = jsonp000044000015229 (callback parameter of the request address)
// Jsonp global function
Window [jsonp] = window [jsonp] | function (tmp ){
Data = tmp;
Success ();
Complete ();
// Garbage collect
Window [jsonp] = undefined;