Javascript 传递两个参数时出现获取承诺错误
我正在尝试用这个方法获取数据,它工作得很好。但现在我想将状态代码发送到下一个then块,但当我按如下方式发送时,承诺失败,responseJson在控制台中的一些垃圾值失败。如何尝试将两个参数发送到下一个模块Javascript 传递两个参数时出现获取承诺错误,javascript,rest,react-native,fetch,Javascript,Rest,React Native,Fetch,我正在尝试用这个方法获取数据,它工作得很好。但现在我想将状态代码发送到下一个then块,但当我按如下方式发送时,承诺失败,responseJson在控制台中的一些垃圾值失败。如何尝试将两个参数发送到下一个模块 fetch(NEW_URL, options) .then((response) => { //console.log(response.status); // Will show you the s
fetch(NEW_URL, options)
.then((response) => {
//console.log(response.status); // Will show you the status
if(response.status === 400) {
reject('A unknown error has occurred. Please try again later.');
}
if (!response.ok) {
reject("server error.");
}
return [response, response.status];
})
.then(([response, status])=>{
return [response.json(), status]
})
.then(([responseJson, status]) => {
//status received here but responseJson is a failed promise
console.log("RESPONSE ", JSON.stringify(responseJson) +" "+NEW_URL);
resolve(responseJson);
}).catch((message)=> {
reject(message)
});
我得到类似于响应{“40:0”,“_65:0”,“_55:null”,“_72:null}https://ap....
在控制台中打印
有人能帮帮我吗。提前感谢尝试此解决方案
fetch(NEW_URL, options)
.then((response) => {
//console.log(response.status); // Will show you the status
if (response.status === 400) {
reject("A unknown error has occurred. Please try again later.");
}
if (!response.ok) {
reject("server error.");
}
return [response, response.status];
})
.then(([response, status]) => {
//Try this solution
//You need to convert response to JSON format otherwise you get dumb values
response.json().then((responseData) => {
console.log("RESPONSE ", responseData + " " + NEW_URL);
});
})
.then(([responseJson, status]) => {
//status received here but responseJson is a failed promise
// console.log("RESPONSE ", JSON.stringify(responseJson) + " " + NEW_URL);
// resolve(responseJson);
})
.catch((message) => {
reject(message);
});
尝试此解决方案
fetch(NEW_URL, options)
.then((response) => {
//console.log(response.status); // Will show you the status
if (response.status === 400) {
reject("A unknown error has occurred. Please try again later.");
}
if (!response.ok) {
reject("server error.");
}
return [response, response.status];
})
.then(([response, status]) => {
//Try this solution
//You need to convert response to JSON format otherwise you get dumb values
response.json().then((responseData) => {
console.log("RESPONSE ", responseData + " " + NEW_URL);
});
})
.then(([responseJson, status]) => {
//status received here but responseJson is a failed promise
// console.log("RESPONSE ", JSON.stringify(responseJson) + " " + NEW_URL);
// resolve(responseJson);
})
.catch((message) => {
reject(message);
});
也许您需要执行另一个responseJson.json()
reject
??看起来您在fetch中使用了承诺构造函数模式,这是一种反模式。也许您需要执行另一个responseJson.json()reject
??看起来您在fetch中使用了承诺构造函数模式,这是一种反模式。