【问题标题】:$.get, $.post, $.ajax, $(elm).load to .ashx page problem$.get, $.post, $.ajax, $(elm).load to .ashx 页面问题
【发布时间】: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" 那是因为你错误地调用了ajaxapi.jquery.com/jQuery.ajax 不过不用担心,get无论如何,post 只是它的包装器,您可以将其从实验中删除。出于好奇,$.get(url + "?load", function (r1) { alert("get: " + r1); }); 会发生什么? (注意我已经更改了查询字符串。)

标签: jquery ashx


【解决方案1】:

嗯,你的$.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 这是正确的,并最终解释了为什么$.ajax 永远不会返回。
【解决方案2】:

由于您一次向同一页面发出四个异步请求,因此这可能是相关的:

【讨论】:

  • 谢谢,我尝试单独调用它们。结果是一样的。
猜你喜欢
  • 1970-01-01
  • 2013-07-02
  • 2012-09-17
  • 1970-01-01
  • 2013-07-21
  • 2010-12-21
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
相关资源
最近更新 更多