1
我目前嚴重依賴Javascript中的原生「承諾」對象進行AJAX調用。但是,正如https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise中提到的,以及在IE11中測試後,Native Promises在IE中不起作用。將原生承諾轉換爲JQuery的承諾(適用於IE)
現在,解決方案是使用JQuery的Promises,但我無法使它工作。 請查找本地無極下面的代碼:(工程在IE之外的所有瀏覽器)
var getJSON = function (url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
console.log(window.location);
console.log(window.location.origin);
xhr.open('get', url, true);
xhr.withCredentials = true;
xhr.responseType = 'application/json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
getJSON(url).then(function (data) {
console.log(data);
console.log("Success");
}, function (status) { //error detection....
console.log(status);
alert('Something went wrong.');
});*/
我想這樣的事情,但它沒有工作
var promise = $.ajax({
url: url,
beforeSend: function (xhr) {
xhr.withCredentials = true;
}
}).done(function (data){
console.log(data);
});
我認爲,我們必須使用推遲的對象,但無法弄清楚! 注:不能使用填充工具或藍鳥由於缺乏的Node.js的 感謝
['$ .getJSON()'](http://api.jquery.com/jquery.getJSON/) – Andreas
不要。你不會對jQuery感到滿意。改爲使用適當的polyfill。 – Bergi
如果您爲本地承諾編寫了大量代碼,那麼爲本地承諾獲取polyfill會容易得多。 jQuery承諾(至少在3.0之前)在語法和行爲方面都非常不標準。一個普通的polyfill或一個增強的polyfill(如Bluebird)可以在不改變任何現有代碼的情況下爲您工作。 – jfriend00