Use of getJSON in asp.net in Jquery

Source: Internet
Author: User

Preparations
· Customer class

Copy codeThe 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

Copy codeThe Code is as follows:
Customer customer = new Customer
{Unid = 1, CustomerName = "", Memo = "Tian kuixing", Other = "sanlang "};
String strJson = Newtonsoft. Json. JsonConvert. SerializeObject (customer );
Context. Response. Write (strJson );

Copy codeThe 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
Copy codeThe Code is as follows:
Customer 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 );



Copy codeThe 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 );
});
}

(3) request the aspx File
· Cs files
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Customer customer = new Customer
{Unid = 1, CustomerName = "", Memo = "Tian kuixing", Other = "sanlang "};
String strJson = Newtonsoft. Json. JsonConvert. SerializeObject (customer );
Response. Write (strJson );
}



· Aspx File
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Json_1.aspx.cs"
Inherits = "webdata_Json_1" %>

The foreground file only retains the Page declaration, and all others are deleted.

· Js files

Copy codeThe Code is as follows:
Function GetCustomer_Aspx (){
$. GetJSON (
"Webdata/Json_1.aspx ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
Tt + = k + ":" + v + "<br/> ";
})
$ ("# Divmessage" 2.16.html (tt );
});
}

This part is the same as when requesting the ashx file.
The request object set is the same as that of ashx.
(4) Request text files
A text file provides a json string, which is a json object obtained by $. getJSON.
· Text files
{Unid: 1, CustomerName: "", Memo: "Tian kuixing", Other: ""}
Text files provide json strings. For the json format, see other documents. Empty rows and spaces are ignored for this entity json.

Copy codeThe Code is as follows:
Function GetCustomer_txt (){
$. GetJSON (
"Webdata/Json_1.txt ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
Tt + = k + ":" + v + "<br/> ";
})
$ ("# Divmessage" 2.16.html (tt );
});
}

The resolution method is the same as that of other methods.

For multiple rows:
Text:
Copy codeThe Code is as follows:
[
{Unid: 1, CustomerName: "", Memo: "Tian kuixing", Other: "sanlang "},
{Unid: 2, CustomerName: "", Memo: "Tianji Star", Other: ""}
]

Resolution:
Copy codeThe Code is as follows:

Function GetCustomer_TxtList (){
$. GetJSON (
"Webdata/Json_1.txt ",
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
$. Each (v, function (kk, vv ){
Tt + = kk + ":" + vv + "<br/> ";
});
});
$ ("# Divmessage" 2.16.html (tt );
});
}

Same as others.
(5) ajax request with Parameters
Take ashx as an example to request customers by Customer id.
· Ashx File
Copy codeThe Code is as follows:
If (context. Request ["iUnid"] = null)
Return;
Context. Response. ContentType = "text/plain ";
Customer 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 );

Int iCustomerId = Convert. ToInt32 (context. Request ["iUnid"]);
Var cus = from q in _ list
Where q. Unid = iCustomerId
Select q;
String strJson = Newtonsoft. Json. JsonConvert. SerializeObject (cus );
Context. Response. Write (strJson );

· Ajax request

Copy codeThe Code is as follows:
Function GetCustomer_AshxWithPara (){
$. GetJSON (
"Webdata/Json_2.ashx ",
{IUnid: 1 },
Function (data ){
Var tt = "";
$. Each (data, function (k, v ){
$. Each (v, function (kk, vv ){
Tt + = kk + ":" + vv + "<br/> ";
});
});
$ ("# Divmessage" 2.16.html (tt );
});
}

The parameter is also in the k/v format. The returned request is displayed as follows: The returned result is from the list of customers on the server side.

In the jquery library, getJSON is actually called: Query. get (url, data, callback, "json ")
This is important.

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.