Thursday, April 28, 2011

jQuery ajax post to web service

$(document).ready(function() {
        $.ajax({ type: "POST",
                        url: "/getprojects.ashx",
                        data: "<formData client=\"\" year=\"\" categories=\"\" tags=\"\" freeText=\"\" count=\"34\" page=\"1\"></formData>",
                        dataType: "text/xml",
                        cache: false,
                        error: function() { alert("No data found."); },
                        success: function(xml) {
                            alert("it works");
                            alert($(xml).find("project")[0].attr("id"));
                        }
        });
    });

My problem is i get some data back but i can't seem to get it displayed.

From stackoverflow
  • Your dataType seems to be wrong. It should look like

    dataType: "xml"
    

    Your data structure also looks pretty wierd. Have a look at .serializeArray(). It should be standard query string foo=bar&test=bla etc.

    If the success handler gets executed, try to lookup your xml variable plain, without operating on it with .find() or whatever. Still empty?

    Buckley : By changing to dataType: "xml" it hits the error handler
    jAndy : @Buckley: see my post again, your `data` is also incorrect. If it still executes the error handler, lookup the error code/string by passing `function(xhr, textStatus, error)`
    Buckley : Thanks jAndy, im having a bit of trouble with the .serializeArray()
    Buckley : I get a [object XMLHttpRequest parsererror TypeError: a is null] with function(xhr, textStatus, error)
  • dataType should be the type of what you receive but contentType should be the mime-type of what you are sending, the following should be ok:

    $(document).ready(function() {
            $.ajax({ type: "POST",
                            url: "/getprojects.ashx",
                            data: "<formData client=\"\" year=\"\" categories=\"\" tags=\"\" freeText=\"\" count=\"34\" page=\"1\"></formData>",
                            contentType: "text/xml",
                            dataType: "xml",
                            cache: false,
                            error: function() { alert("No data found."); },
                            success: function(xml) {
                                alert("it works");
                                alert($(xml).find("project")[0].attr("id"));
                            }
            });
        });
    
    Buckley : Thanks but for some reason it hits the error handler
    Claude Vedovini : the error callback signature is error(XMLHttpRequest, textStatus, errorThrown) what are the values of the parameters?
    Buckley : [object XMLHttpRequest parsererror TypeError: a is null] but ive kinda given up and move on but an answer on how to get it working would still be appreciated.
    Claude Vedovini : The question now is whether the issue is while sending or receiving data. Do your server receive the request and what is its response?

0 comments:

Post a Comment