Example one, using the $.getjson method of jquery
Front-end JS:
$ ("#selId"). Click (function () {
$.Getjson("Http://www.example.com/remote.php?callback=?", Callback=? Parameters are focused and will be passed to the background for automatic processing
{name: ' max ' Age: 23 Str: ' hello ' },
function (data {
console.logdata }
);
});
/span>
//back-end remote.php
$callback = $_request[' callback '); The front desk passed it over? is automatically parsed into a function name by jquery.
$arr=Array(' Name '=' Hello ', ' age ' =, ' male ' = ' female ', ' str '= >$_request[' str '];
$tmp = json_encode($arr);
echo $callback." ($tmp) "; //In essence, backstage here just need to return "Funname ({name: ' Max ', age:23} JSON data)" in the format of the string is all Right
Example two, using the $.ajax method of jquery
Front-End Code:
$ ("#selId"). Click (function () {
$. Ajax({
Type: "GET",
The Asyn default is true, which means that an asynchronous request is used; false means that a synchronization request is used, the synchronization request locks the browser, and the user's other actions wait for the request to complete before they can execute
async: false
url: " Http://www.example.com/remote.php ", // Cross-domain background files, mainly generated strings;
datatype: "JSONP" Span class= "pun" >, //get
Jsonp jsonp method : , //is used to obtain the name of the JSONP callback function name, generally callback
/** a custom Jsonp callback function name, at which time the background return string is echo "Remotehandler (JSON)"; jquery is randomly generated automatically, and the backend code is $callback =$_request[' callback ', Echo $callback. Jsonstr.
*/
Jsonpcallback: "Remotehandler",
Success: function(json) {
Console. Log(json);
},
});
});
Backstage remote.php
$arr = Array (' name ' = = ' Hello ', ' age ' =>23, ' male ' = ' female ', ' str ' =>$_request[' str ');
$tmp = Json_encode ($arr);
echo "Remotehandler ($tmp)"; // Remotehandler with the front door jsonpcallback: "Remotehandler" Correspondence
JSONP accessing data across domains