Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 585
Jan Johansen Avatar asked Jun 23 '10 06:06

Jan Johansen


2 Answers

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"));
                        }
        });
    });
like image 84
Claude Vedovini Avatar answered Nov 17 '22 09:11

Claude Vedovini


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?

like image 32
jAndy Avatar answered Nov 17 '22 09:11

jAndy