即使404,fetch也会解析? [英] fetch resolves even if 404?

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

问题描述

使用此代码:

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 ?

我的意思是 - 接下来呢? a fetch 到服务器停机并仍然获得解决的承诺?

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

我知道我可以在回复的 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 ).

BTW,jquery request ,确实被拒绝了

推荐答案

A 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.status !== 200) {
        // make the promise be rejected if we didn't get a 200 response
        throw new Error("Not 200 response")
    } 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆