[英]jQuery AJAX Call Triggers Error Function
I am using this ajax call in my code, but it triggers the error function everytime. 我在代码中使用了这个ajax调用,但是每次都会触发错误函数。 Anyone have any idea why this is happening?
有人知道为什么会这样吗?
$.ajax({
type:'GET',
url: 'https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml',
datatype: 'xml',
success: function(xml){
console.log(xml);
},
error: function(err){
alert("ERROR!");
}
});
To my understanding, the syntax looks correct. 据我了解,语法看起来是正确的。 Can someone help me to see why this triggers an error, rather than placing the xml into my console?
有人可以帮我看看为什么会触发错误,而不是将xml放入控制台吗? Thanks.
谢谢。
I also see this in the console: XMLHttpRequest cannot load https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml. Origin null is not allowed by Access-Control-Allow-Origin.
我还在控制台中看到了这一点:
XMLHttpRequest cannot load https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml. Origin null is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml. Origin null is not allowed by Access-Control-Allow-Origin.
You need to use jsonp to do a cross domain request with ajax - which means you can't request XML using jQuery's ajax method. 您需要使用jsonp来使用ajax进行跨域请求-这意味着您无法使用jQuery的ajax方法来请求XML。 Here are other related questions.
这是其他相关问题。
cross domain issue with Jquery Jquery的跨域问题
How to Parse XML Cross-domain in jQuery? 如何在jQuery中解析XML跨域?
You can use Yahoo API library (YQL) to to get the xml though 您可以使用Yahoo API库(YQL)来获取xml
Source from http://www.cypressnorth.com/blog/programming/cross-domain-ajax-request-with-xml-response-for-iefirefoxchrome-safari-jquery/ 来自http://www.cypressnorth.com/blog/programming/cross-domain-ajax-request-with-xml-response-for-iefirefoxchrome-safari-jquery/
// Accepts a url and a callback function to run.
function requestCrossDomain(site, callback) {
// If no url was passed, exit.
if (!site) {
alert('No site was passed.');
return false;
}
// Take the provided url, and add it to a YQL query. Make sure you encode it!
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
// Request that YSQL string, and run a callback function.
// Pass a defined function to prevent cache-busting.
$.getJSON(yql, cbFunc);
function cbFunc(data) {
// If we have something to work with...
if (data.results[0]) {
if (typeof callback === 'function') {
callback(data);
}
}
// Else, Maybe we requested a site that doesn't exist, and nothing returned.
else throw new Error('Nothing returned from getJSON.');
}
}
function xmlSuccess(data){
console.log(data.results[0]);
}
requestCrossDomain('https://s3.amazonaws.com/GSTArticles/GoogleMaps/Landmarks.xml',xmlSuccess);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.