Why am I getting this error when trying to read a remote xml feed?
XMLHttpRequest cannot load http://www.companyname.com/external.php?type=xml. Origin http://intranet is not allowed by Access-Control-Allow-Origin.
This is the script I am using to try to get the xml file:
$(document).ready(function() {
get_xml_feed();
function get_xml_feed() {
$.ajax({
url: 'http://www.companyname.com/external.php?type=xml',
type: 'GET',
dataType: 'xml',
error: function(xhr, status, error) {
console.log(status);
console.log(xhr.responseText);
},
success: function(xml) {
$(xml).find('items').each(function(){
var id = $(this).attr('guid');
var title = $(this).find('title').text();
var date = $(this).find('pubDate').text();
var url = $(this).find('link').text();
$('.divContent').empty().append(title + " - " + date + " - " + url + "<br />");
});
}
});
}
});
Because you are violating the same origin policy. AJAX requests can be sent only to urls belonging on the same domain as the one hosting your script.
Typical workaround consist of setting up a server side script on your domain that will act as a bridge between your domain and the remote domain and then send an AJAX request to this script that will delegate.
Another possibility is to use JSONP but this the remote domain must support it.