HTML页面
// in script tag
$(document).ready(function () {
var url = "list.ashx";
$.get(url + "?get", function (r1) { alert("get: " + r1); });
$.post(url + "?post", function (r2) { alert("post: " + r2); });
$.ajax(url + "?ajax", function (r3) { alert("ajax: " + r3); });
$("div:last").load(url + "?load", function (r4) { alert("load: " + r4); });
});
// in body tag
<div></div>
public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }
结果
问题是
更新
$("input").click(function () {
$.ajax({ url: url
, context: this
, data: "ajax=test"
, cache: false
, async: false
, global: false
, type:"POST"
, processData: false
, dataType: "html"
, success: function (data) { alert(data); }
, error: function (data) { alert(data.responseText); }
});
});
总是遇到错误:function(){}但是'data.responseText'是正确的结果!!
答案 0 :(得分:6)
嗯,$.ajax()
不起作用的原因是因为它是syntactically invalid。看起来应该更像这样:
$.ajax({
type: "POST", // or "GET"
url: "list.ashx",
data: "postvar=whatever",
success: function(r3){
alert("ajax: " + r3);
}
});
此外,使用$.get
和$.post
时,您应该将数据放在第二个参数中:
$.get(url, 'getvar=whatever', function (r1) { alert("get: " + r1); });
$.post(url, 'postvar=whatever', function (r2) { alert("post: " + r2); });
// or use a map
$.get(url, { getvar : 'whatever' }, function (r1) { alert("get: " + r1); });
$.post(url, { postvar : 'whatever' }, function (r2) { alert("post: " + r2); });
答案 1 :(得分:1)
由于您在同一页面中一次性触发了四个异步请求,因此这可能是相关的: