我知道這已經被訪問了很多,我已經設法讓這個工作與其他項目。我繼承了這段在IE中不起作用的代碼塊:與IE的JS CORS問題 - 如何解決?
function loadTemplate(template, alt_cache_name) {
if (templates[template] !== undefined) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = templates[template];
}
return templates[template];
} else {
return $.get(template, function (response) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = response;
}
templates[template] = response;
}, 'text');
}
}
問題出在$ .get()上。
template
的值是特定html模板的網址。
我嘗試以下,但我不認爲返回類型是一樣的東西從$獲得()返回:
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function loadTemplate(template, alt_cache_name) {
if (templates[template] !== undefined) {
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = templates[template];
}
return templates[template];
} else {
var request = createCORSRequest("GET", template);
var content = "";
if (request) {
request.onload = function() {
//do something with request.responseText
console.log(request.responseText);
if (alt_cache_name !== undefined) {
templates[alt_cache_name] = response.responseText;
}
templates[template] = response.responseText;
};
}
return request;
// return $.get(template, function (response) {
// if (alt_cache_name !== undefined) {
// templates[alt_cache_name] = response;
// }
// templates[template] = response;
// }, 'text');
}
}
我甚至試過$.get
有:
jQuery.support.cors = true;
有人可以闡明如何讓這個工作?