Jquery. getjson details and getjson and asp tutorials. net instances
Jquery. getjson (url, [data], [callback]) loads json data across domains.
Url: the address where the request is sent
Data: (optional) key/value parameter to be sent
Callback: (optional) callback function for successful Loading
It is mainly used by clients to obtain server json data. Simple Example:
The server script returns json data:
$. Getjson. php tutorial
$ Arr = array ("name" => "zhangsan", "age" => 20 );
$ Jarr = json_encode ($ arr );
Echo $ jarr;
Note:
First, encode the data to be returned using the php json_encode function before returning to the client.
Second, echo rather than return is returned to the client.
The core client code is as follows:
Pai.getjson.html
The Code is as follows:
<Script language = "webpage effect" 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" id = "btn" value = "test" onclick = "javascript: getjs ();"/>
The following describes the getjson application in the asp.net tutorial.
Preparations
· Customer class
The Code is as follows:
Public class customer
{
Public int unid {get; set ;}
Public string customername {get; set ;}
Public string memo {get; set ;}
Public string other {get; set ;}
}
(1) ashx
The Code is as follows:
Customer = new customer
{Unid = 1, customername = "", memo = "Tian kuixing", other = "sanlang "};
String strjson = newtonsoft. json. jsonconvert. serializeobject (customer );
Context. response. write (strjson );
The Code is as follows:
Function getcustomer_ashx (){
$. Getjson (
"Webdata/json_1.ashx ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
Tt + = k + ":" + v + "<br/> ";
})
$ ("# Divmessage" 2.16.html (tt );
});
}
· Request data from ashx through getjson. The returned data is a json object.
(2) The ashx file, but the returned object set
The Code is as follows:
Customer = new customer
{Unid = 1, customername = "", memo = "Tian kuixing", other = "sanlang "};
Customer customer2 = new customer
{Unid = 2, customername = "", memo = "", other = ""};
List <customer> _ list = new list <customer> ();
_ List. add (customer );
_ List. add (customer2 );
String strjson = newtonsoft. json. jsonconvert. serializeobject (_ list );
Context. response. write (strjson );
The Code is as follows:
Function getcustomerlist (){
$. Getjson (
"Webdata/json_1.ashx ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
$. Each (v, function (kk, vv ){
Tt + = kk + ":" + vv + "<br/> ";
});
});
$ ("# Divmessage" 2.16.html (tt );
});
}
1 2 3