$.get、$.post、$.ajax、$(elm).load到.ashx页面问题
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>
'list.ashx' 中的
public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }
结果
- $.get 和 $.post 到达 list.ashx 但 没有返回
- $.ajax 未到达 list.ashx
- $.load 完全成功
问题是
- 为什么只有 '$.load' 工作?
- 如何使 $.get 或 $.post 工作?
更新
$("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”是正确的结果!
HTML page
// 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>
in 'list.ashx'
public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }
the result
- $.get and $.post reach list.ashx but
no return - $.ajax not reach list.ashx
- $.load fully success
The problems are
- why only '$.load' work?
- how to make $.get or $.post work?
update
$("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); }
});
});
it's always hit error:function(){} but the 'data.responseText' is the correct result!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您的
$.ajax()
不起作用的原因是它的 语法上无效。它应该看起来更像这样:另外,当使用
$.get
和$.post
时,您应该将数据放在第二个参数中:Well, the reason your
$.ajax()
doesn't work is because it's syntactically invalid. It should look more like this:Also, when using
$.get
and$.post
, you should put the data in the second parameter:由于您一次向同一页面发出四个异步请求,因此这可能是相关的:
Since you are firing off four asynchronous requests in one go to the same page, this might be relevant: