jquery ajax 封装方法
function AjaxMethod() {
//this.init.apply(this, arguments);
}
AjaxMethod.prototype = {
init: function() {
debugger;
},
GetJson: function() {
jQuery.getJSON(
"Json.ashx",
{ name: 'test', age: 32 },
function(data) {
debugger;
var txt = eval(data);
//var obj = data.toJSONString(); //由JSON字符串转换为JSON对象
var objs = JSON.stringify(data); //由JSON字符串转换为JSON对象
alert(txt);
})
},
GetAjax: function() {
jQuery.ajax({
url: "Json.ashx",
type: "get",
dataType: "json",
contextType: "application/json; charset=utf-8",
data: { name: 'test', age: 32 },
success: function(data) {
debugger;
jQuery.each(data, function(i) {
});
},
error: function() {
//请求出错处理
alert(1);
}
})
},
PostAjax: function() {
jQuery.post(
"Json.ashx",
{
name: userName,
age: 12
// ajaxMethod: "Login"
},
function(data) {
var d = data;
},
"json"
);
}
}
var method=new AjaxMethod();
后台代码:
context.Response.ContentType = "text/plain";
// string data ="{ name:\"ants\",age:24}";//构建的json数据
string data = "[{name:\"fan\",age:26},{name:\"wang\",age:25}]";//构建的json数据
//string data = "{name:test}";
//下面两句是用来测试前台向此页面发出的查询字符
string querystrname = context.Request.QueryString.GetValues("name")[0];//取查询字符串中namer的值
string querystage = context.Request.QueryString.GetValues("age")[0];//取查询字符串中age的值
context.Response.Write(JsonConvert.SerializeObject(data) );