简体   繁体   English

拿来。 处理来自服务器的 500 个响应

[英]fetch. Processing 500 responses from the server

I send this request to the server:我将此请求发送到服务器:

fetch('/changeCountProductInCart', {
            method: 'POST',
            body: JSON.stringify({
                product_id: this.dataset.product_id,
                action: 'changeByInput',
                nodeName: this.nodeName,
                product_count: this.value
            }),
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        }).then(res => {
            if(res.ok) {
                totalAmount();
            } else if(!res.ok) {
                return res.json();
            }
        }).then(body => {
            this.value = body.stock;
        });

I want to go to then only if the response from the server only is not in the range of 200-300,but I just started to delve into promise and can't find the answer to my question只有当服务器的响应不在 200-300 范围内时,我才想去,但我刚刚开始深入研究承诺,但找不到我的问题的答案

PS I will be grateful for any help or hint PS我将不胜感激任何帮助或提示

You need to catch the server error responses:您需要catch服务器错误响应:

fetch('/changeCountProductInCart', {
            method: 'POST',
            body: JSON.stringify({
                product_id: this.dataset.product_id,
                action: 'changeByInput',
                nodeName: this.nodeName,
                product_count: this.value
            }),
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }
        })
        .catch(err => /* error getting server response*/);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM