0

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 :-)

Lamp
  • 1,084
  • 1
  • 14
  • 27
  • 1
    See http://stackoverflow.com/questions/1077218/are-different-ports-on-the-same-server-considered-cross-domain-ajax-wise – Crescent Fresh Oct 26 '10 at 17:52
  • Can you post your controller that is generating the XML and also Post the output of firebug (is he sending the request to the server and if so what is the response) – Max Schulze Oct 26 '10 at 18:23

3 Answers3

0

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
  • 1,002
  • 1
  • 12
  • 22
  • firebug console output: GET http://localhost:3000/users/mike.xml 200 OK 298ms jquery.min.js (line 130) ParamsHeadersPostPutResponseCacheHTMLXML (an empty string) – Lamp Oct 26 '10 at 18:54
  • When i call from browser http://localhost:3000/users/mike.xml my xml returns normal – Lamp Oct 26 '10 at 18:57
  • If you look at the headers for the empty response, what's the Content-Type? Content-Length? – mrusinak Nov 03 '10 at 20:48
0

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
  • 8,202
  • 13
  • 58
  • 95
  • 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) – Lamp Oct 26 '10 at 20:55
  • Does your controller do format.xml { render: => @object.to_xml } or does it send down an xml template? – Samo Oct 26 '10 at 21:04
0

If the URL is not on the same domain as your page with the JavaScript, your browser will prevent it from working because of the cross domain security policy. Check that you are rendering the page via a URL that looks exactly the same as the one you are requesting in your ajax call (so in the localhost example make sure you aren't running your server on a 127.0.0.1 URL, or something like that)

Peter
  • 29,498
  • 21
  • 89
  • 122