te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How do I determine the status code of a Fetch request? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I determine the status code of a Fetch request? - Stack Overflow

programmeradmin3浏览0评论

I want to fetch an URL and determine what status code it has, especially if it has an HTTP 301 or HTTP 302 status code:

fetch('', {
    redirect: 'true'})
    .then(response => {
       console.log(response);
       const headers = response.headers.entries();
       for (h of headers) {
        console.log(h);
        }
    }).catch(err => {
        console.log("in error case")
        console.log(err);
    })

This does give me a lot of information, especially it tells me via:

response.redirected 

Which is true. But this only tells me that it redirected, not what the status code was.

Is there a way to use Fetch API to determine the real status code of the request?

I want to fetch an URL and determine what status code it has, especially if it has an HTTP 301 or HTTP 302 status code:

fetch('https://httpstat.us/301', {
    redirect: 'true'})
    .then(response => {
       console.log(response);
       const headers = response.headers.entries();
       for (h of headers) {
        console.log(h);
        }
    }).catch(err => {
        console.log("in error case")
        console.log(err);
    })

This does give me a lot of information, especially it tells me via:

response.redirected 

Which is true. But this only tells me that it redirected, not what the status code was.

Is there a way to use Fetch API to determine the real status code of the request?

Share Improve this question edited Mar 15, 2022 at 9:34 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Sep 1, 2017 at 16:03 Franz EnzenhoferFranz Enzenhofer 3,8165 gold badges21 silver badges30 bronze badges 3
  • 1 Can't you use Response.status? – Andrew Li Commented Sep 1, 2017 at 16:14
  • 1 I don't think this is possible – Benjamin Gruenbaum Commented Sep 1, 2017 at 16:35
  • @andrewLi Response.status tells the status of the final destination, not of the requested URL – Franz Enzenhofer Commented Sep 14, 2017 at 13:38
Add a ment  | 

2 Answers 2

Reset to default 8

As far as why the code can’t just check response.status: In the case of redirects, that will be the status code of whatever response ultimately es back as the result of any redirects.

So if redirects end up taking things somewhere that returns a 200, then response.status will just be 200. No intermediate status codes are exposed to frontend JavaScript running in a browser—as the previous (very good) answer posted here already points out.

This does give me a lot of information, especially it tells me via:

response.redirected

It’s worth noting: to just detect if there’s been a redirect, you actually don’t even need to check that. That’s the easiest way to check, but you could also just check response.url, and if that’s different from the request URL you gave to the fetch(…) call, you know it was redirected.

That used to be the typical way to check before response.redirected was first introduced.

Also worth noting: redirect: 'true' in the code in the question isn’t valid; it’ll cause the code to fail prematurely. See https://developer.mozilla/en-US/docs/Web/API/Request/redirect:

Value A RequestRedirect enum value, which can be one the following strings:

  • follow
  • error
  • manual

So what the code in the question seems to intend is redirect: 'follow'. But that’s actually the default for redirect, so you don’t need to explicitly specify it and can instead just omit it.

As far as the other redirect values: As explained in an answer to another question here at Stack Overflow, you almost certainly never want to specify manual except for some Service Worker cases. And error means to treat any redirect as a network error.

In that case, for the code in the example, redirect: 'true' would cause the catch to get hit and you’d have no access to any details of the response, including whether it got redirected.

So to loop back to your original question:

I want to fetch an URL and determine what status code it has, especially if it has an HTTP 301 or HTTP 302 status code

…the answer is that for redirected requests there really is no way from frontend JavaScript code running in a browser to detect the exact status codes of any redirect.

So if you need to know the exact status code for any redirects that may happen for a request to a given URL, you must make the request from backend code, and handle the response there.

OK first to answer your question no you cannot determine the difference between 301 and 302 without some changes in the backend.(Follow this SO dicussion for backend solution).

The other thing you can do is to set redirect to 'error' in which cases fetch will throw an error on all redirects and you can handle what happens if it was a redirect.

The main reason why this is like that is (from my understanding) is because the difference between 301 and 302 is mainly useful to browsers which needs to handle how subsequent request should behave (whether to permanently change the cache or just this request).

But in terms of the user the spec specifies both cases to handled transparently meaning you need to know there is a redirect but what status code caused the redirect does not matter. Hence only the response.redirected flag. But if you still need to please follow the above backend solution.

发布评论

评论列表(0)

  1. 暂无评论