0

I have the following jQuery

$.ajax({
    type: "GET",
    url: "http://f.cl.ly/items/0i1V1L1k2F440L1m2Y0G/pointdata.xml",
    dataType: "xml",
    success: parseXml
});

function parseXml(xml) {
    $(xml).find('point').each(function() {
        var lat = $(this).children('lat').text();
        var long = $(this).children('long').text();
        alert(lat + long);
    });
}

Trying to read data from this XML file

You can also see a live jsFiddle here

For some reason, the variables lat and long aren't being assigned for each element. What am I doing wrong? Any help would be much appreciated. Thanks in advance. ​

6
  • Let me guess. http://f.cl.ly/ is a third party domain. Commented Jun 24, 2012 at 12:37
  • The jsfiddle doesn't work because of the same origin policy restriction which forbids you from sending cross domain AJAX requests. Are you sure that you didn't violate this policy in your actual code? So unless your script is hoisted on http://f.cl.ly/ you cannot send AJAX requests to it. Commented Jun 24, 2012 at 12:37
  • Updated it, by adding it as a resource, but still no luck. jsfiddle.net/STe25/2 Commented Jun 24, 2012 at 12:39
  • @jacktheripper When in doubt, always add an error callback and investigate the Ajax error you get. Commented Jun 24, 2012 at 12:44
  • 1
    @jacktheripper, no you didn't add it as resource. You just added a reference to it. The file is still located on f.cl.ly and your updated jsfiddle doesn't work because you are getting a 404 error. Please use FireBug to see those errors. So as everyone already stated here: you cannot send cross domain AJAX calls. Commented Jun 24, 2012 at 12:45

1 Answer 1

3

Your parseXML function works

You're probably violating the same origin policy.
You can't send ajax requests to other domains.

Note that lat + long concats strings so '1' + '2' is '12' not 3.
If you want the result to be 3, parse to int first.

Sign up to request clarification or add additional context in comments.

Comments

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.