Preparation work
·Customer class
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
Customer customer = new Customer
{ Unid=1,CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Black Saburo"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);
function GetCustomer_Ashx() {
$.getJSON(
"webdata/Json_1.ashx",
function( data) {
var tt = "";
$.each(data, function(k, v) {
tt = k ":" v "
";
})
$("#divmessage").html(tt);
});
}
·Request data from ashx through getJSON. The returned data is a JSON object.
(2) ashx file, but returns an entity collection
Customer customer = new Customer
{ Unid=1,CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Black Saburo"};
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "Wu Yong", Memo = "Tianjixing", Other = "Zhiduoxing" };
List _list = new List();
_list.Add(customer) ;
_list.Add(customer2);
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list);
context.Response.Write(strJson);
function GetCustomerList() {
$. getJSON(
"webdata/Json_1.ashx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
$ .each(v,function(kk, vv) {
tt = kk ":" vv "
";
});
});
$("# divmessage").html(tt);
});
}
(3) Request aspx file
·cs file
protected void Page_Load(object sender, EventArgs e)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "Song Jiang", Memo = "Tian Kuixing", Other = "Hei 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" %>
Only the Page declaration is retained in the front-end file, and all others are deleted.
·js file
function GetCustomer_Aspx() {
$.getJSON(
"webdata/Json_1.aspx",
function(data) {
var tt = "";
$.each(data, function (k, v) {
tt = k ":" v "
";
})
$("#divmessage").html(tt);
} );
}
This part is the same as when requesting ashx file.
When requesting an entity collection, it is the same as ashx, so there will be no repetition here.
(4) Request text file
The text file provides a json string, and the json object is obtained through $.getJSON.
·Text file
{Unid:1,CustomerName:"Song Jiang",Memo:"Tian Kuixing",Other:"Black Saburo"}
The text file provides a json string. For the json composition format, please See other documentation. For this entity json, blank lines and spaces will be ignored.
function GetCustomer_txt() {
$.getJSON(
"webdata/Json_1.txt",
function(data) {
var tt = "";
$ .each(data, function(k, v) {
tt = k ":" v "
";
})
$("#divmessage").html(tt );
});
}
The parsing method is the same as others.
For multi-line as follows:
Text:
[
{Unid:1,CustomerName:"Song Jiang",Memo:"Tiankuixing",Other:"Hei Sanlang"},
{Unid:2,CustomerName:"Wu Yong", Memo:"Tianjixing",Other:"Zhiduoxing"}
]
Analysis:
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 "
";
});
});
$("#divmessage").html(tt);
});
}
Same as others.
(5) Ajax request with parameters
Take ashx as an example to request customers based on customer ID.
·Ashx file
if(context.Request[ "iUnid"]==null)
return;
context.Response.ContentType = "text/plain";
Customer customer = new Customer
{ Unid = 1, CustomerName = "Song Jiang", Memo = "Tiankuixing", Other = "Hei Sanlang" };
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "Wu Yong", Memo = "Tianjixing", Other = "Zhiduoxing" };
List _list = new List();
_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
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 "
";
});
});
$("#divmessage").html(tt);
});
}
The parameters are also sent in k/v pair format. You can see what is returned by the request: it is returned as a Customer list collection on the server side.
In the jquery library, getJSON is actually called: Query.get(url, data, callback, "json")
This is very important.