A common Getjson in jquery is to invoke and get a remote JSON string, convert it to a JSON object, and, if successful, execute a callback function. The prototype is as follows:
Jquery.getjson (URL, [data], [callback]) loads JSON data across domains. Yichuan County Second Secondary school
- URL: Sending the requested address
- Data: (optional) key/value parameter to be sent
- Callback: (optional) callback function when loading succeeds
Primarily used by clients to obtain server JSON data. Simple example:
Server script that returns JSON data:
View Source print?
2 |
$arr = array ( "name" => "zhangsan" , "age" =>20); |
3 |
$jarr =json_encode( $arr ); |
Note two: first: Before returning to the client, use the PHP function Json_encode to encode the data to be returned. Second: ECHO is used instead of return to the client.
The following is the core client code:
View Source print?
01 |
<script language= "javascript" type= "text/javascript" src= "./js/jquery.js" ></script> |
02 |
<script language= "javascript" type= "text/javascript" > |
05 |
$.getJSON( "$.getJSON.php" , {}, function (response){ |
10 |
<input type= "button" name= "btn" id= "btn" value= "test" onClick= "javascript:getjs();" /> |
Since the value is returned in PHP with JSON encoding, it is necessary to use Getjson to invoke the PHP file to get the data. It can also be noted that the data obtained through Getjson has become an array of objects, which can be intuitively used to get the return value response.name,response.age.
jquery provides a $.getjson approach that allows us to implement cross-domain AJAX requests, but there is too little content on JQUERYAPI, how to use $.getjson to request that a Web site should return a database for $.getjson to get, I'll use a practical example to illustrate the following.
The backend is PHP, the following code is the main implementation of a function is to provide an appointment registration interface, need to pass in the data are: User name, contact number and address:
View Source print?
02 |
case "yuyue_interface" : |
03 |
$name = trim( $_GET [ ‘name‘ ]); |
04 |
$phone = trim( $_GET [ ‘phone‘ ]); |
05 |
$addr = trim( $_GET [ ‘addr‘ ]); |
06 |
$dt = date ( "Y-m-d H:i:s" ); |
07 |
$cb = $_GET [ ‘callback‘ ]; |
08 |
if ( $name == "" || $name == NULL){ |
09 |
echo $cb . "({code:" .json_encode(1). "})" ; |
10 |
} elseif ( $phone == "" || $phone == NULL){ |
11 |
echo $cb . "({code:" .json_encode(2). "})" ; |
12 |
} elseif ( $addr == "" || $addr == NULL){ |
13 |
echo $cb . "({code:" .json_encode(3). "})" ; |
15 |
$db ->execute( "insert into tb_yuyue (realname,telphone,danwei,dt,ischeck) values (‘$name‘,‘$phone‘,‘$addr‘,‘$dt‘,0)" ); |
16 |
echo $cb . "({code:" .json_encode(0). "})" ; |
Then there is the front-end processing:
View Source print?
01 |
$(document).ready( function (){ |
03 |
var name = "name" ; //varchar类型,长度最多为8位(4个汉字) |
04 |
var phone = "phone" ; //varchar类型,长度为11位 |
05 |
var addr = "addr" ; //varchar类型,长度最多为500位(250个汉字) |
06 |
$.getJSON( "http://请求网站地址/data.php?ac=yuyue_interface&name=" +name+ "&phone=" +phone+ "&addr=" +addr+ "&callback=?" , function (data){ |
10 |
} else if (data.code==2){ |
13 |
} else if (data.code==3){ |
It is important to note that in the backend PHP code, the "&callback=" must be passed in. "also lost, such as:
View Source print?
1 |
$cb = $_GET [ ‘callback‘ ]; |
2 |
echo $cb . "({code:" .json_encode(4). "})" ; |
The above is a simple $.getjson experiment, through which we can learn how to use $.getjson, and also learn how to make an interface for other people to cross-domain requests.
Launching cross-domain AJAX requests with jquery's $.getjson