繁体   English   中英

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

[英]fetch. Processing 500 responses from 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;
        });

只有当服务器的响应不在 200-300 范围内时,我才想去,但我刚刚开始深入研究承诺,但找不到我的问题的答案

PS我将不胜感激任何帮助或提示

您需要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