$.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出错!");
}
});
});
*/