Sample code for cross domain Ajax requests using $. Getjson

Time:2021-7-30

In jQuery, getjson is often used to call and obtain the remote JSON string and convert it into a JSON object. If successful, the callback function will be executed. The prototype is as follows:

JQuery. Getjson (URL, [data], [callback]) loads JSON data across domains.

URL: address to send the request
Data: (optional) key / value parameter to be sent
Callback: (optional) callback function upon successful loading

It is mainly used for client to obtain server JSON data. Simple example:

Server script, return JSON data:

Copy codeThe code is as follows:
// $.getJSON.php

$arr=array(“name”=>”zhangsan”, “age”=>20);
$jarr=json_encode($arr);
echo $jarr;

Note two points: first, use the PHP function JSON before returning to the client_ Encode encodes the data to be returned. Second: echo instead of return is used to return to the client.

The following is the core client code:

Copy codeThe code is as follows:
<script language=”javascript” type=”text/javascript” src=”./js/jquery.js”></script>
<script language=”javascript” type=”text/javascript”>
function getjs()
{
$.getJSON(“$.getJSON.php”, {}, function(response){
alert(response.age);
});
}
<input type=”button” name=”btn” value=”test” onClick=”javascript:getjs();”/>

Since the return value is encoded in JSON in PHP, you must use getjson to call the PHP file to obtain data. At the same time, it can be noted that the data obtained through getjson has become an object array, and the return value can be obtained intuitively with response.name and response.age.

JQuery provides a method of $. Getjson so that we can implement cross domain Ajax requests, but there is too little content on the jQuery API. How to use $. Getjson and what database the requesting website should return in order to get $. Getjson, I will use a practical example to illustrate below.

The back-end is in PHP. One of the main functions of the following code is to provide an interface for reservation registration. The data to be passed in are: user name, contact phone and address:

Copy codeThe code is as follows:
/*Reservation registration execution interface*/
case “yuyue_interface”:
$name = trim($_GET[‘name’]);
$phone = trim($_GET[‘phone’]);
$addr = trim($_GET[‘addr’]);
$dt = date(“Y-m-d H:i:s”);
$cb = $_GET[‘callback’];
if($name == “” || $name == NULL){
echo $cb.”({code:”.json_encode(1).”})”;
}elseif($phone == “” || $phone == NULL){
echo $cb.”({code:”.json_encode(2).”})”;
}elseif($addr == “” || $addr == NULL){
echo $cb.”({code:”.json_encode(3).”})”;
}else{
$db->execute(“insert into tb_yuyue (realname,telphone,danwei,dt,ischeck) values (‘$name’,’$phone’,’$addr’,’$dt’,0)”);
echo $cb.”({code:”.json_encode(0).”})”;
}
exit;
break;

Then comes the front-end processing:

Copy codeThe code is as follows:
$(document).ready(function(){
//The following three parameters are required for reservation registration
var name = “name”; // Varchar type, with a maximum length of 8 digits (4 Chinese characters)
var phone = “phone”; // Varchar type, 11 bits in length
var addr = “addr”; // Varchar type, with a maximum length of 500 characters (250 Chinese characters)
$. Getjson (“http: / / request website address / data. PHP? AC = Yuyue_ interface&name=”+name+”&phone=”+phone+”&addr=”+addr+”&callback=?”, function(data){
if(data.code==1){
//Custom code
Alert (“name cannot be empty”);
}else if(data.code==2){
//Custom code
Alert (“mobile phone cannot be empty”);
}else if(data.code==3){
//Custom code
Alert (“the company cannot be empty”);
}else{
//Custom code
Alert (“appointment succeeded”);
}
});
});

It should be noted that in the back-end PHP code, you must pass in “& callback =?” Also output, such as:

Copy codeThe code is as follows:
$cb = $_GET[‘callback’];
echo $cb.”({code:”.json_encode(4).”})”;

The above is a simple $. Getjson experiment. Through this experiment, we can learn how to use $. Getjson and how to make an interface for others to cross domain requests.