本文介绍了从deferred.then()返回数据时保留参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 deferred.then 回调中返回数据时,如何传递多个参数?
How can I pass more than 1 argument when returning data in a deferred.then callback?
var data = {
json: JSON.stringify({
text: 'some text',
array: [1, 2, 'three'],
object: {
par1: 'another text',
par2: [3, 2, 'one'],
par3: {}
}
}),
delay: 3
};
$.ajax({
url:'/echo/json/',
data: data,
type: 'POST'
}).then(function(response, statusText, jqXhr){
response.text = 'foo';
// how to return the rest of the arguments correctly?
return response;
}).done(function(response, statusText, jqXhr){
console.log(response); // <- altered response
console.log(statusText); // <- how to pass it along?
console.log(jqXhr); // <- how to pass it along?
});
推荐答案
您将需要返回 deferred 使用 resolveWith
方法解析为多个值.当然,返回单个(但复合)值通常更干净.
You will need to return a deferred that is resolved with multiple values using the resolveWith
method. Of course, returning a single (but composite) value is usually cleaner.
$.ajax(…).then(function(response) {
response.text = 'foo';
return $.Deferred().resolveWith(this, arguments); // if you don't replace but modify vals
// alternatively, .resolveWith(this, [response, …]);
}).done(function(response, statusText, jqXhr) {…});
这篇关于从deferred.then()返回数据时保留参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!