6
$(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.

3
  • How does this data look like? What part of it do you want to extract? How and where do you want it formated and displayed? Commented Jun 23, 2010 at 6:31
  • The data i get looks like this: <content> <outcome success="1" requestId="" command="" /> <projects totalNumberOfProjects="226" selectedNofProjects="34" curPage="1" nOfPages="7"> <project id="378" title="Campaign" confidential="1" client="Client1" image="" thumb="" publicLink="" internalLink="" /> </projects> </content> I some of the attributes on all the projects, displayed in html. Commented Jun 23, 2010 at 6:43
  • if you used alternating single quotes/double quotes you wouldn't need to escape them all :( Commented Feb 11, 2016 at 15:42

2 Answers 2

10

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"));
                        }
        });
    });
Sign up to request clarification or add additional context in comments.

4 Comments

the error callback signature is error(XMLHttpRequest, textStatus, errorThrown) what are the values of the parameters?
[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.
The question now is whether the issue is while sending or receiving data. Do your server receive the request and what is its response?
For this, can you please show me any sample WebMethod?
2

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?

3 Comments

@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)
Thanks jAndy, im having a bit of trouble with the .serializeArray()
I get a [object XMLHttpRequest parsererror TypeError: a is null] with function(xhr, textStatus, error)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.