Home  >  Article  >  Web Front-end  >  jquery cross-domain request example sharing (jquery sends ajax request)_jquery

jquery cross-domain request example sharing (jquery sends ajax request)_jquery

WBOY
WBOYOriginal
2016-05-16 16:54:341236browse

getJSON is commonly used in jQuery to call and obtain the remote JSON string, convert it into a JSON object, and if successful, execute the callback function. The prototype is as follows:

jQuery.getJSON( url, [data], [callback] ) loads JSON data across domains.

url: the address to send the request
data: (optional) key/value parameters to be sent
callback: (optional) callback function when loading is successful
is mainly used for the client to obtain the server JSON data. Simple example:

Server script, return JSON data:

Copy code The code is as follows:

// $.getJSON.php
$arr =array("name"=>"zhangsan", "age"=>20);
$jarr=json_encode($arr);
echo $jarr;

Note two points: First: Before returning to the client, use the PHP function json_encode to encode the returned data. Second: echo is used to return to the client, not return.

The following is the core client code:

Copy code The code is as follows: