Home > Web Front-end > JS Tutorial > Jquery Ajax request code (2)_jquery

Jquery Ajax request code (2)_jquery

WBOY
Release: 2016-05-16 18:12:36
Original
1240 people have browsed it

Just add the reference Dll file and you can use it, which is very convenient. /201101/tools/Newtonsoft.Json.Net20.rar
In the jquery library, getJSON is actually called: Query.get(url, data, callback, "json")
where Parameters are also sent in k/v pair format. You can see what the request returns: the server returns a Customer list collection
Now let’s look at the sequence of events:
Part a Common 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; }
}

Write the following method in the general processing file (ashx)
Copy code The code is as follows:

Customer customer = new Customer
{ Unid=1 ,CustomerName="Song Jiang",Memo="Tian Kuixing",Other="Hei Sanlang"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson) ;


Load the calling ashx code in the front-end Jquery
Request data from ashx through getJSON. The returned data is a JSON object
Copy code The code is as follows:

$().ready( function() {
$.getJSON("JqueryData2.ashx", function(data) {
alert(data.Memo);
});
$.getJSON("JqueryData2.ashx" , function(data) {
var tt = "";
$.each(data, function(k, v) {
tt = k ":" v "
";
})
$("#disHows").html(tt);
});
});

(2) ashx file, but return is the entity collection
Copy code The code is as follows:

Customer customer = new Customer
{ Unid=1,CustomerName="Song Jiang",Memo="Tian Kuixing",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);
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list);
context.Response.Write(strJson);

function GetCustomerList() {
$.getJSON(
"JqueryData2.ashx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
$.each(v,function (kk, vv) {
tt = kk ":" vv "
";
});
});
$("#divmessage").html( tt);
});
}
[code]
(3) Request CS in aspx file
[code]
protected void Page_Load(object sender, EventArgs e)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "Song Jiang", Memo = "Tiankuixing", Other = "Black Saburo" };
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" %>
Idea: Only keep the Page statement in the front-end file, and delete all others
Jquery code
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);
});
}

(4) Request text File
The text file provides a json string, and the json object is obtained from $.getJSON
·Text file
{Unid: 1, CustomerName: "Song Jiang", Memo: "Tian Kuixing", Other: "Hei Sanlang "}
The text file provides a json string. For the composition format of json, 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 that of ashx
The format for multiple lines in the Txt file is as follows:
Text content:
[
{Unid:1,CustomerName:"Song Jiang",Memo :"Tiankuixing",Other:"Herosaburo"},
{Unid:2,CustomerName:"Wu Yong",Memo:"Tianjixing",Other:"Zhiduoxing"}
]
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);
});
}

/201101/tools/Newtonsoft.Json.Net20.rar
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template