0
我不明白,諾言/遞延非常......我有類似的東西:承諾延期AJAX API jQuery的
function callAjax() {
return $.ajax({
type: 'post',
dataType: 'jsonp',
data: {status: status, name: name},
url: '/test',
xhrFields: {
withCredentials: true
}
});
}
function connectAjax() {
var msg = 'doesnt work';
var promise = callAjax();
promise.then(function(data, textStatus, xhr) {
msg = 'it worked !';
}, function(data, textStatus, xhr) {
msg = 'it failed !';
});
console.log(msg); // output 'doesnt work'
}
我已經嘗試了很多不同的東西(總,做等。 ),但無法使其工作。
我使用jsonp但我的請求不是跨域。我希望服務器發出500個錯誤請求。
Ajax請求的異步執行。 '.then()'將在ajax執行成功/失敗。 console.log()將在** ajax完成之前執行**。所以毫不奇怪'msg'是「不起作用」。在互聯網上閱讀JavaScript中的異步編程。 – Regent 2014-10-27 13:38:58