unitario
unitario

Reputation: 6535

What is wrong with with my jQuery AJAX request?

I am trying to make an jQuery $ajax GET request to an ASPX-page returning an XML document but is not able to get it to work. What am I doing wrong?

jQuery

$(document).ready(function() {
  $("#loading").show();
  $.ajax({
    type: "GET",
    url: "http://www.url.com/reports/xml.aspx",
    dataType: "xml",
    success: parseXml
});

function parseXml(xml) {
    $(xml).find("Year").each(function() {
    $("body").append( $(this).find("RevenueYear").text() + '<br />' });
}});​

HTML

<!DOCTYPE html>
<html>
   <head>
   </head>
   <body>

   </body>
</html>

XML

<root>
   <Year>
      <RevenueYear>2011</RevenueYear>
      <RevenueType>actual</RevenueType>
   </Year>
   <Year>
      <RevenueYear>2012</RevenueYear>
      <RevenueType>estimate</RevenueType>
   </Year>
</root>

Upvotes: 1

Views: 201

Answers (2)

user57508
user57508

Reputation:

Credit goes to BNL, who asked the crucial question!

According to the jQuery-documentation of jQuery.ajax()

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

Same-Origin-policy in depth

Some time ago, there was a similar question here on SO - the solution is pretty simple: Just create a web-proxy (PHP, ASP.NET, ...) to transfer the content @ server-side, so you avoid the same-origin-policy.

Upvotes: 2

gen_Eric
gen_Eric

Reputation: 227310

Your parseXml function has a few syntax errors. You have the wrong number of } and ).

It should be:

function parseXml(xml) {
    $(xml).find("Year").each(function() {
        $("body").append( $(this).find("RevenueYear").text() + '<br />');
    });
}

Upvotes: 1

Related Questions