【发布时间】:2011-05-25 21:48:37
【问题描述】:
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); }
});
});
它总是命中 error:function(){} 但 'data.responseText' 是正确的结果!!
【问题讨论】:
-
Firebug“网络”标签或 Chrome 的资源跟踪器应该会告诉您更多关于请求会发生什么的信息。
-
"$.ajax 无法到达 list.ashx" 那是因为你错误地调用了
ajax:api.jquery.com/jQuery.ajax 不过不用担心,get无论如何,post只是它的包装器,您可以将其从实验中删除。出于好奇,$.get(url + "?load", function (r1) { alert("get: " + r1); });会发生什么? (注意我已经更改了查询字符串。)