<tfoot id='sjILl'></tfoot>
  1. <small id='sjILl'></small><noframes id='sjILl'>

  2. <legend id='sjILl'><style id='sjILl'><dir id='sjILl'><q id='sjILl'></q></dir></style></legend>

      • <bdo id='sjILl'></bdo><ul id='sjILl'></ul>

    1. <i id='sjILl'><tr id='sjILl'><dt id='sjILl'><q id='sjILl'><span id='sjILl'><b id='sjILl'><form id='sjILl'><ins id='sjILl'></ins><ul id='sjILl'></ul><sub id='sjILl'></sub></form><legend id='sjILl'></legend><bdo id='sjILl'><pre id='sjILl'><center id='sjILl'></center></pre></bdo></b><th id='sjILl'></th></span></q></dt></tr></i><div id='sjILl'><tfoot id='sjILl'></tfoot><dl id='sjILl'><fieldset id='sjILl'></fieldset></dl></div>

      无法使用 JavaScript 获取响应状态代码

      时间:2023-10-02
      <tfoot id='wqWSy'></tfoot>
      <legend id='wqWSy'><style id='wqWSy'><dir id='wqWSy'><q id='wqWSy'></q></dir></style></legend>
        <i id='wqWSy'><tr id='wqWSy'><dt id='wqWSy'><q id='wqWSy'><span id='wqWSy'><b id='wqWSy'><form id='wqWSy'><ins id='wqWSy'></ins><ul id='wqWSy'></ul><sub id='wqWSy'></sub></form><legend id='wqWSy'></legend><bdo id='wqWSy'><pre id='wqWSy'><center id='wqWSy'></center></pre></bdo></b><th id='wqWSy'></th></span></q></dt></tr></i><div id='wqWSy'><tfoot id='wqWSy'></tfoot><dl id='wqWSy'><fieldset id='wqWSy'></fieldset></dl></div>

            <bdo id='wqWSy'></bdo><ul id='wqWSy'></ul>

            1. <small id='wqWSy'></small><noframes id='wqWSy'>

                <tbody id='wqWSy'></tbody>

              • 本文介绍了无法使用 JavaScript 获取响应状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试创建一个登录表单.当我用 Postman 测试服务时,我会得到一个带有状态码等的 body 对象.

                I'm trying to create a login form. when I'm testing the service with Postman, I will get a body object with status code and etc.

                但是,使用 JavaScript 获取,我无法获取 body 对象,我刚刚收到一个错误:

                But, with JavaScript fetch, I can't get body object and I just received an error:

                export const login = (username,password) => {
                    return dispatch=>{
                        const basicAuth = 'Basic ' + btoa(username + ':' + password);
                        let myHeaders = new Headers();
                            myHeaders.append('Authorization', basicAuth);
                            myHeaders.append('Content-Type', 'application/json');      
                            fetch(`${baseUrl}api/user/login`, {
                                withCredentials: true,
                                headers: myHeaders
                            })
                            .then(function (response) {
                                return response.json();
                            })
                            .then(function (json) {
                                dispatch(setLoginInfo(json))
                            })
                            .catch(err =>{
                                console.log(err)
                                 dispatch(loginFailed())
                            });
                    }
                
                }
                

                我需要获取状态码.

                推荐答案

                状态码是 status 属性在响应对象上.此外,除非您在 error 响应中使用 JSON(当然有些人会这样做),否则您需要检查状态代码(或 ok flag) 在调用 json 之前:

                The status code is the status property on the response object. Also, unless you're using JSON with your error responses (which some people do, of course), you need to check the status code (or the ok flag) before calling json:

                fetch(`${baseUrl}api/user/login`, {
                    withCredentials: true,
                    headers: myHeaders
                })
                .then(function(response) {
                    console.log(response.status); // Will show you the status
                    if (!response.ok) {
                        throw new Error("HTTP status " + response.status);
                    }
                    return response.json();
                })
                .then(// ...
                

                不检查请求是否成功是一个常见的错误,我 写了它上我贫血的小博客.

                Not checking that the request succeeded is such a common mistake I wrote it up on my anemic little blog.

                这篇关于无法使用 JavaScript 获取响应状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:fetch、ajax 和 xhr 之间的区别 下一篇:发送自定义用户代理字符串以及我的标头(获取)

                相关文章

                  <bdo id='tAkiU'></bdo><ul id='tAkiU'></ul>
                1. <legend id='tAkiU'><style id='tAkiU'><dir id='tAkiU'><q id='tAkiU'></q></dir></style></legend>

                    <small id='tAkiU'></small><noframes id='tAkiU'>

                    <tfoot id='tAkiU'></tfoot>
                  1. <i id='tAkiU'><tr id='tAkiU'><dt id='tAkiU'><q id='tAkiU'><span id='tAkiU'><b id='tAkiU'><form id='tAkiU'><ins id='tAkiU'></ins><ul id='tAkiU'></ul><sub id='tAkiU'></sub></form><legend id='tAkiU'></legend><bdo id='tAkiU'><pre id='tAkiU'><center id='tAkiU'></center></pre></bdo></b><th id='tAkiU'></th></span></q></dt></tr></i><div id='tAkiU'><tfoot id='tAkiU'></tfoot><dl id='tAkiU'><fieldset id='tAkiU'></fieldset></dl></div>