JQuery. getJSON details with getJSON and asp.net instances (1/3)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.