[英]how to know the state of a response with fetch Api javascript?
function boleta(){
var check = false;
var formboleta = new FormData();
formboleta.append("num_boleta", data2.boletas[id].num_boleta);
formboleta.append("created_at", data2.boletas[id].created_at);
formboleta.append("total", data2.boletas[id].total);
//arreglo id productos
for(let k = 0; k < arr.length; k++){
formboleta.append("productos", data2.boletas[id].productos[k].id);
}
var requestOptions = {
method: 'POST',
body: formboleta,
redirect: 'follow'
};
let status;
fetch("url_goes_here", requestOptions)
.then((response) => {
status = response.status;
return response.json();
})
.catch(error => console.log('error', error));
//---------------------------------------------
}
Hi guys, i want to know how to get the state of a response, to make an if and execute another function if the state is 200 or success.大家好,我想知道如何获得响应的 state,如果 state 为 200 或成功,则进行 if 并执行另一个 function。
in the first response i want to make if status is ok, call another function which in this case it will be function cliente();.如果状态正常,我想在第一个响应中调用另一个 function,在这种情况下它将是 function cliente();。
The function cliente(); function cliente(); will do another post and create a customer for this ticket, but i can't call it when the ticket( by ticket i mean the function boleta, which inside creates a boleta(ticket)) isn't done already.
将做另一个帖子并为此票创建一个客户,但是当票(票我的意思是 function boleta,它在里面创建一个boleta(票))尚未完成时,我无法调用它。
You can get the status of an HTTP request as您可以获得 HTTP 请求的状态为
.then((response) => {
status = response.status;
return response.json();
})
If you somehow want status in second then
, then create a variable and store the status after getting the response like如果您想在第二个获得状态,
then
创建一个变量并在获得响应后存储状态,例如
let status; fetch("https://jsonplaceholder.typicode.com/todos/1").then((response) => { // Get status using response.status status = response.status; console.log(`status in first then ${status}`); return response.json(); }).then((json) => { // Get status in after the first.then console.log(`status in second then 4 ${status}`); console.log(console.log(json)); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.