$ .get,$。postt,$ .ajax,$(elm).load to .ashx page problem

时间:2010-12-15 13:01:27

标签: jquery 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 not reach list.ashx
  • $。加载完全成功

问题是

  • 为什么只有'$ .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'是正确的结果!!

2 个答案:

答案 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)

由于您在同一页面中一次性触发了四个异步请求,因此这可能是相关的: