Preparatory work
· Customer class
Copy Code code 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;}
}
(i) ashx
Copy Code code as follows:
Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Days kuixing", other= "black Three Lang"};
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (customer);
Context. Response.Write (Strjson);
Copy Code code as follows:
function Getcustomer_ashx () {
$.getjson (
"Webdata/json_1.ashx",
function (data) {
var tt = "";
$.each (data, function (k, v) {
tt + + k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});
}
• Request data from Getjson to ASHX. The returned data is a JSON object.
(ii) ASHX file, but returns a collection of entities
Copy Code code as follows:
Customer customer = new Customer
{unid=1,customername= "Song Jiang", memo= "Days kuixing", other= "black Three Lang"};
Customer Customer2 = new Customer
{Unid = 2, CustomerName = "Wu use", Memo = "Secret Star", other = "Sundance"};
list<customer> _list = new list<customer> ();
_list. ADD (customer);
_list. ADD (CUSTOMER2);
String Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (_list);
Context. Response.Write (Strjson);
Copy Code code 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"). HTML (TT);
});
}
(iii) Request ASPX file
CS File
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kuixing", other = "Black Three Lang"};
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 retains only the page declaration, and all others are deleted.
js File
Copy Code code as follows:
function getcustomer_aspx () {
$.getjson (
"Webdata/json_1.aspx",
function (data) {
var tt = "";
$.each (data, function (k, v) {
tt + + k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});
}
This section is the same as when the Ashx file is requested.
When a collection of entities is requested, it is the same as ashx, where no duplication is made.
(iv) Request for a text file
The text file provides a JSON string, and the $.getjson gets the JSON object.
• Text File
{unid:1,customername: "Song Jiang", Memo: "Heaven Kuixing", Other: "Black Three Lang"}
The text file provides a JSON string, and for the form of JSON, see other documentation. For this entity JSON, blank lines and spaces are ignored.
Copy Code code as follows:
function Getcustomer_txt () {
$.getjson (
"Webdata/json_1.txt",
function (data) {
var tt = "";
$.each (data, function (k, v) {
tt + + k + ":" + V + "<br/>";
})
$ ("#divmessage"). HTML (TT);
});
}
The method of parsing is the same as the others.
For multiple lines, the following:
Text:
Copy Code code as follows:
[
{unid:1,customername: "Song Jiang", Memo: "Heaven Kuixing", Other: "Black Three Lang"},
{unid:2,customername: "Wu uses", Memo: "Secret Star", Other: "Sundance"}
]
Analytical:
Copy Code code 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"). HTML (TT);
});
}
The same as it is.
(v) AJAX requests with parameters
In the case of ashx, the customer is requested by the customer ID.
· ASHX file
Copy Code code as follows:
if (context. request["Iunid"]==null)
Return
Context. Response.ContentType = "Text/plain";
Customer customer = new Customer
{Unid = 1, CustomerName = "Song Jiang", Memo = "Day Kuixing", other = "Black Three Lang"};
Customer Customer2 = new Customer
{Unid = 2, CustomerName = "Wu use", Memo = "Secret Star", other = "Sundance"};
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 Code code 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"). HTML (TT);
});
}
Where the parameters are also emitted in k/v format. The request returned can be seen on the server side, returned in the Customer list collection.
In the jquery library, Getjson is actually invoked: Query.get (URL, data, callback, "JSON")
This is very important.