Reputation: 6535
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
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.
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
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