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

Bon*_*ton 2 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>
Run Code Online (Sandbox Code Playgroud)

在'list.ashx'中

public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }
Run Code Online (Sandbox Code Playgroud)

结果

  • $ .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); }
                });
        });
Run Code Online (Sandbox Code Playgroud)

它始终是命中错误:function(){}但是'data.responseText'是正确的结果!!

Ste*_*hen 6

那么,你$.ajax()不工作的原因是因为它在语法上是无效的.它应该看起来更像这样:

$.ajax({
    type: "POST", // or "GET"
    url: "list.ashx",
    data: "postvar=whatever",
    success: function(r3){
       alert("ajax: " + r3);
    }
});
Run Code Online (Sandbox Code Playgroud)

此外,使用$.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); });
Run Code Online (Sandbox Code Playgroud)