$.ajax({
type: "post", url: "Ajax1.ashx",
data: { i1: $("#txt1").val(), i2: $("#txt2").val() },
success: function (data, txtStatus) {alert(data);},
error: function () { alert("错误"); }
});
$(function () {
$.ajax({
type:"post",url:"jQueryAjaxTest.ashx",
data:{i1:10,i2:30},
success: function (resTxt) {
//alert(resTxt);
var nums=$.parseJSON(resTxt);//json字符串转化为javascript对象 (不建议用这个,这个太麻烦!)
for (var i = 0; i < nums.length; i++) {
alert(nums[i]);
}
},
error:function(){
alert("ajax出错!");
}
});
});
#### 以上这种使用的$.parseJson()把字符串解析为JavaScript对象,但是比eval()更安全 ####
$(function () {
$.ajax({
type: "post", url: "jQueryAjaxTest.ashx",
dataType:"json", //这里从服务器中拿到的json字符串,通过这一语句设置后,就是间接地通过了paseJson()方法来变成了javascript对象
data: { i1: 10, i2: 30 },
success: function (resTxt) {
for (var i = 0; i < resTxt.length; i++) {
alert(resTxt[i]);
}
},
error: function () {
alert("ajax出错!");
}
});
});
//第三种方法是在ashx文件中,修改报文头。。context.Response.ContentType = "application/json";
$(function () {
$.ajax({
type: "post", url: "jQueryAjaxTest.ashx",
data: { i1: 10, i2: 30 },
success: function (resTxt) {
for (var i = 0; i < resTxt.length; i++) {
alert(resTxt[i]);
}
},
error: function () {
alert("ajax出错!");
}
});
});
*/
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.ContentType = "application/json";
//做一个加法的运算
//int i1 = Convert.ToInt32(context.Request["i1"]);
//int i2 = Convert.ToInt32(context.Request["i2"]);
//context.Response.Write(i1+i2);
int[] ints = new int[] { 12,14,5566};
JavaScriptSerializer jss = new JavaScriptSerializer();
string jo = jss.Serialize(ints);
context.Response.Write(jo);
}