fetch 即使 404 也能解决?

2023-10-02前端开发问题
54

本文介绍了fetch 即使 404 也能解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用此代码:

fetch('notExists') // <---- notice 
    .then(
        function(response)
        {
           alert(response.status)
        }
    )
    .catch(function(err)
    {
       alert('Fetch Error : ', err);
    });

这个承诺解决.

mdn

它返回一个可以解析为该请求的响应的承诺,不管成功与否.

It returns a promise that resolves to the Response to that request, whether it is successful or not.

一个失败的 ajax 请求即使转到一个不存在的资源也得到解决,这不是很奇怪吗?

Isn't it strange that a failed ajax request is resolved even if it goes to an non-existing resource ?

我的意思是——接下来呢?fetch 到已关闭但仍获得已解决承诺的服务器?

I mean - what next ? a fetch to a server which is down and still get a resolved promise ?

我知道我可以在 response 对象的 ok 属性中进行调查,但仍然 -

I know I can investigate at the ok property at the response object , but still -

问题

为什么会为完全错误的请求(非现有资源)解决提取问题.

Why do a fetch gets resolved for a completely bad request ( non existing resource ).

顺便说一句,jquery 请求确实被拒绝了

推荐答案

fetch() 调用只有在网络请求本身由于某种原因失败(找不到主机、没有连接、服务器没有响应,等等...).

A fetch() call is only rejected if the network request itself fails for some reason (host not found, no connection, server not responding, etc...).

从承诺的角度来看,从服务器返回的任何结果(404、500 等...)都被认为是成功的请求.从概念上讲,您从服务器发出请求,服务器响应您,因此从网络的角度来看,请求成功完成.

Any result back from the server (404, 500, etc...) is considered a successful request from the promise point of view. Conceptually, you made a request from the server and the server answered you so from the networking point of view, the request finished successfully.

然后您需要测试该成功响应以查看是否有您想要的答案类型.如果您希望 404 被拒绝,您可以自己编写代码:

You need to then test that successful response to see if has the type of answer you wanted. If you want a 404 to be a rejection, you could code that yourself:

fetch('notExists').then(function(response) {
    if (!response.ok) {
        // make the promise be rejected if we didn't get a 2xx response
        const err = new Error("Not 2xx response");
        err.response = response;
        throw err;
    } else {
         // go the desired response
    }
}).catch(function(err) {
    // some error here
});

您甚至可以制作自己的 myFetch(),它会自动为您执行此操作(将任何非 200 响应状态转换为拒绝).

You could even make your own myFetch() that just does this automatically for you (converts any non-200 response status to a rejection).

已解决的承诺背后的原因是什么?请求(非现有资源/服务器停机).

What is the reason behind a resolved promise for a a completely bad request ( non existing resource / server down).

首先,服务器关闭不会产生成功的响应 - 会拒绝.

First off, server down will not generate a successful response - that will reject.

如果您成功连接到服务器,向其发送请求并返回响应(任何响应),则会生成成功响应.至于为什么"fetch() 接口的设计者决定基于此拒绝,如果不与实际参与该接口设计的人交谈,这有点难以说,但对我来说这似乎是合乎逻辑的.通过这种方式,拒绝会告诉您请求是否通过并得到有效响应.由您的代码决定如何处理响应.当然,您可以创建自己的包装函数来修改默认行为.

A successful response is generated if you successfully connect to the server, send it a request and it returns a response (any response). As for "why" the designers of the fetch() interface decided to base the rejection on this, it's a bit hard to say without talking to someone who was actually involved in the design of that interface, but it seems logical to me. This way the rejection tells you whether the request got through and got a valid response. It's up to your code to decide what to do with the response. You can, of course, create your own wrapper function that modifies that default behavior.

这篇关于fetch 即使 404 也能解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165