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

浪尽此生 提交于 2019-12-08 02:32:15

问题


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


回答1:


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



回答2:


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?


来源:https://stackoverflow.com/questions/4450139/get-post-ajax-elm-load-to-ashx-page-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!