views:

29

answers:

2

I have a rails app and i wanna to parse the xml response from API using Javascript (ajax). I tried:

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "http://localhost:3000/users/mike.xml",                                
    dataType: "xml",
    success: parseXml

  });
});

function parseXml(xml)
{
...
}

but don't work. When i am changing the 'url' with a local xml file e.x url: 'data.xml', works fine!

How can i parse this response?

any help will be highly appreciated :-)

A: 

As Max suggested, it would be very helpful to install and use Firebug in a Firefox browser so you can watch the GET request and response. There isn't a lot to go on from your question, but it sounds like a problem on the "server" end.

When you say it doesn't work, do you mean parseXml(xml) isn't called? In your AJAX request, you define a success handler - if your GET request is failing then that handler is never called. I've found it's more useful to define the general callback ('complete' for JQuery, which it kind of looks like you're using) since it will get called no matter what the response. Then you just check to for success or failure yourself and take appropriate action.

mrusinak
i mean that doesn't return any xml response..
vic
firebug console output: GET http://localhost:3000/users/mike.xml 200 OK 298ms jquery.min.js (line 130)ParamsHeadersPostPutResponseCacheHTMLXML(an empty string)
vic
When i call from browser http://localhost:3000/users/mike.xml my xml returns normal
vic
A: 

Try removing the ".xml" from your URL. I believe AJAX calls accept xml response by default, so having xml in your request header as well as your URL might be confusing the controller. That's just a guess though.

Samo
nothing..It seems that the only way is jsonp (json with padding).I can grab this response but with 1error in my firebug console...(invalid label)
vic
Does your controller do format.xml { render: => @object.to_xml } or does it send down an xml template?
Samo