Home  >  Article  >  Web Front-end  >  Jquery Ajax请求代码(2)_jquery

Jquery Ajax请求代码(2)_jquery

WBOY
WBOYOriginal
2016-05-16 18:12:361148browse

添加引用Dll文件就可以使用了,很方便的。 /201101/tools/Newtonsoft.Json.Net20.rar
在jquery库中,getJSON其实是调用的:Query.get(url, data, callback, "json")
其中参数也是以k/v对格式发出。请求返回的可以看到:在服务端以Customer列表集合返回
现在来看一下事列:
件一个Common类

复制代码 代码如下:

public class Customer
{
public int Unid { get; set; }
public string CustomerName { get; set; }
public string Memo { get; set; }
public string Other { get; set; }
}

在一般处理文件(ashx)中写一个如下方法
复制代码 代码如下:

Customer customer = new Customer
{ Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);


在前台Jquery中加载调用ashx代码
通过getJSON向ashx请求数据。返回的数据为JSON对象
复制代码 代码如下:

$().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);
});
});

(二)ashx文件,但返回的是实体集合
复制代码 代码如下:

Customer customer = new Customer
{ Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};
Customer customer2 = new Customer
{ Unid = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" };
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]
(三)请求aspx文件中的CS
[code]
protected void Page_Load(object sender, EventArgs e)
{
Customer customer = new Customer
{ Unid = 1, CustomerName = "宋江", Memo = "天魁星", Other = "黑三郎" };
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
Response.Write(strJson);
}

·Aspx文件
Inherits="webdata_Json_1" %>
主意:前台文件只保留Page声明,其它全部删除
Jquery代码
复制代码 代码如下:

function GetCustomer_Aspx() {
$.getJSON(
"webdata/Json_1.aspx",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt += k + ":" + v + "
";
})
$("#divmessage").html(tt);
});
}

(四)请求文本文件
文本文件提供json字符串,由$.getJSON得到json对象
·文本文件
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"}
文本文件提供json串,对于json的组成格式,对于这一实体json,会被忽略空行与空格
复制代码 代码如下:

function GetCustomer_txt() {
$.getJSON(
"webdata/Json_1.txt",
function(data) {
var tt = "";
$.each(data, function(k, v) {
tt += k + ":" + v + "
";
})
$("#divmessage").html(tt);
});
}

解析的方法与ashx的解析相同
在Txt文件中对于多行的格式如下:
文本内容:
[
{Unid:1,CustomerName:"宋江",Memo:"天魁星",Other:"黑三郎"},
{Unid:2,CustomerName:"吴用",Memo:"天机星",Other:"智多星"}
]
复制代码 代码如下:

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