我试图在我的ajax请求的结果上添加一些变量,然后再将其转到.then()回调。但是我的代码总是返回ajax对.then()语句的响应结果。
$.ajax({
url: "api.php", //this returning [{"sid":"454545454554"}]
success:function(data) {
a = data.push({"additional_var" : "one"});
return a;
}
})
.then(function(response1){
console.log(response1);
})
在response1
上方的代码中,总是返回来自我的api.php([{"sid":"454545454554"}]
)的响应。我希望response1
添加{"additional_var" : "one"}
到.then()之前。有什么办法吗?
答案 0 :(得分:0)
将所有内容都放在.then
内:
.then(function(response1){
response1.push({"additional_var" : "one"});
console.log(response1);
})
或将另一个.then
链接到开头(除非代码的其他部分分别使用Promise,否则这不是一个好主意):
const prom = $.ajax({
url: "api.php"
})
.then(function(response1){
response1.push({"additional_var" : "one"});
console.log(response1);
})
prom.then(console.log);
答案 1 :(得分:0)
您可以执行以下操作:
$.ajax({
url: "api.php"
}).then(function(data) {
data.push({
"additional_var": "one"
});
return data;
}).then(function(response1) {
console.log(response1);
})