开发者

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

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!!


Well, the reason your $.ajax() doesn't work is because it's syntactically invalid. It should look more like this:

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

Also, when using $.get and $.post, you should put the data in the second parameter:

$.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); });


Since you are firing off four asynchronous requests in one go to the same page, this might be relevant:

  • How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜