本机 XMLHttpRequest() 成功发出 CORS 请求但 jQuery .ajax 没有?
Native XMLHttpRequest() successfully makes CORS request but jQuery .ajax does not?
使用 vanilla XMLHttpRequest()
对象发出 CORS 请求对我来说成功了,但没有使用 jQuery.get()
函数。然而 $.get()
是建立在 $.ajax()
之上的,而 $.ajax()
是建立在浏览器的 XMLHttpRequest()
对象之上的。
为什么我的 jQuery .get()
告诉我不允许跨源请求?
fiddle: https://jsfiddle.net/3pwhu05t/
jQuery()
jQuery.get( {url: 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf'});
// or
jQuery.get( {url: 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf',
crossDomain: true,
xhrFields: {
withCredentials: true
},
} );
XMLHttpRequest 代码(取自this htlm5rocks.com example)
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Make the actual CORS request.
function makeCorsRequest() {
var url = 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var responseText = xhr.responseText;
console.log(responseText);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
jQuery 3.0 引入了一种使用 jQuery.get
的新方法,允许您传入设置对象。这就是为什么 jsfiddle 在边缘工作,而不是 2.1.4,以及为什么在你的本地机器上它请求本地文件系统从而给你一个 cors 错误。如果要使用 jQuery.get({url: theurl})
语法,则必须使用 jQuery 3.x,否则应使用 jQuery.get(theurl)
使用 vanilla XMLHttpRequest()
对象发出 CORS 请求对我来说成功了,但没有使用 jQuery.get()
函数。然而 $.get()
是建立在 $.ajax()
之上的,而 $.ajax()
是建立在浏览器的 XMLHttpRequest()
对象之上的。
为什么我的 jQuery .get()
告诉我不允许跨源请求?
fiddle: https://jsfiddle.net/3pwhu05t/
jQuery()
jQuery.get( {url: 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf'});
// or
jQuery.get( {url: 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf',
crossDomain: true,
xhrFields: {
withCredentials: true
},
} );
XMLHttpRequest 代码(取自this htlm5rocks.com example)
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Make the actual CORS request.
function makeCorsRequest() {
var url = 'https://www.wikidata.org/wiki/Special:EntityData/Q4231.rdf';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var responseText = xhr.responseText;
console.log(responseText);
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
jQuery 3.0 引入了一种使用 jQuery.get
的新方法,允许您传入设置对象。这就是为什么 jsfiddle 在边缘工作,而不是 2.1.4,以及为什么在你的本地机器上它请求本地文件系统从而给你一个 cors 错误。如果要使用 jQuery.get({url: theurl})
语法,则必须使用 jQuery 3.x,否则应使用 jQuery.get(theurl)