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 p开发者_C百科arse this response?
any help will be highly appreciated :-)
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.
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.
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)
精彩评论