gpt4 book ai didi

javascript - 从 deferred.then() 返回数据时保留参数

转载 作者:行者123 更新时间:2023-12-02 16:09:05 25 4
gpt4 key购买 nike

deferred.then 中返回数据时如何传递超过 1 个参数回调?

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?
});

http://jsfiddle.net/rv1aydvb/

最佳答案

您需要返回deferred使用 resolveWith method 解决多个值。当然,返回单个(但复合)值通常更干净。

$.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) {…});

关于javascript - 从 deferred.then() 返回数据时保留参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com