1

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>
6
  • 2
    Are you getting an error? If so, could you please post it? Commented Jul 30, 2012 at 18:24
  • 2
    is www.url.com the same server as this script runs from? Commented Jul 30, 2012 at 18:26
  • 3
    api.jquery.com/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. (en.wikipedia.org/wiki/Same_origin_policy) Commented Jul 30, 2012 at 18:30
  • @AndreasNiedermair That is the reason I could not get it to work. Commented Jul 30, 2012 at 18:35
  • @klick.klonk there's a similar question (stackoverflow.com/questions/11333885/…) which has a solution in it (php-web-proxy to avoid the same-origin-policy) Commented Jul 30, 2012 at 18:36

2 Answers 2

2

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.

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

Comments

1

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 />');
    });
}

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.