2

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

Bonshington
  • 3,970
  • 2
  • 25
  • 20
  • 2
    The Firebug "net" tab or Chrome's resource tracker should tell you more about what happens to the requests. – Pekka Dec 15 '10 at 13:03
  • 2
    *"$.ajax not reach list.ashx"* That's because you're calling `ajax` incorrectly: http://api.jquery.com/jQuery.ajax/ But don't worry about it, `get` and `post` are just wrappers for it anyway, you can drop it from your experiment. Out of curiousity, what happens to `$.get(url + "?load", function (r1) { alert("get: " + r1); });`? (Note I've changed the query string.) – T.J. Crowder Dec 15 '10 at 13:11

2 Answers2

6

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); });
Stephen
  • 18,827
  • 9
  • 60
  • 98
1

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

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406