Home >Web Front-end >JS Tutorial >Instructions for using getJSON in Jquery in asp.net_jquery

Instructions for using getJSON in Jquery in asp.net_jquery

WBOY
WBOYOriginal
2016-05-16 18:09:42942browse

Preparation work
·Customer class

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

Copy code The code is as follows:

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);

Copy Code 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 "
";
})
$("#divmessage").html(tt);
});
}

·Request data from ashx through getJSON. The returned data is a JSON object.
(2) ashx file, but returns an entity collection
Copy code The code is as follows:

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);



Copy code 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 "
";
});
});
$("# divmessage").html(tt);
});
}

(3) Request aspx file
·cs file
Copy code The code is as follows:

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

Copy code The code is as follows:

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.

Copy code The code is as follows:

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:
Copy code The code is as follows:

[
{Unid:1,CustomerName:"Song Jiang",Memo:"Tiankuixing",Other:"Hei Sanlang"},
{Unid:2,CustomerName:"Wu Yong", Memo:"Tianjixing",Other:"Zhiduoxing"}
]

Analysis:
Copy code The 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 "
";
});
});
$("#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
Copy code The code is as follows:

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

Copy code The 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 "
";
});
});
$("#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.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn