views:

40

answers:

1

Hi I have the following:

         $.ajax({
           type: "POST",
           url: global.pathPrefix + '/services/brochure.asmx/ShowPostRequest',
           data: "parkIds=" + $('input.ids').val(),               
           success: function(msg){
             alert( "Data Saved: " + msg );
           },
           error: function(msg){
             alert( "error: " + msg );
           }               
         });

I am expecting "true" or "false" while I'm getting "[object Document]". How to parse this returned data? on the website of jQuery, there are not much to learn about. I've tried "msg.d" as well which returned nothing.

thanks

+1  A: 

When you use jQuery's .ajax function, what it passes back to your callbacks depends on what kind of response is sent. From the docs:

jQuery will intelligently try to get the results, based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

From the error message you've quoted, it looks like your server-side script is returning XML, and so jQuery is turning that into an XML document instance for you. If you want, you can override jQuery's guessing and tell it how to handle the response, by using the dataType option in the request (dataType: "text", for instance), but of course, if what's coming back is XML, your alerts will show the full XML, not just the content of the particular success element you're after.

More likely, you want to actually use the XML document that was returned, which is after all a nice structured document. jQuery's usual traversal stuff can be used to move around within the XML structure. For instance, let's assume your response looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<foo>
    <success>true</success>
</foo>

Then your success function could do this:

success: function(xml) {
    var success = $(xml).find('success').text();
    alert(success);
}

Here are some articles about using XML with jQuery:

T.J. Crowder
it should be $(xml).find('success').text(); instead but thanks alot...
Arief Iman Santoso
@Arief: Doh! Can't believe I did that. :-) Fixed, thanks.
T.J. Crowder