React Js Axios Post 请求未收到 body 从网络返回 api

React Js Axios Post Request not receiving body back from web api

所以我有一个网络 api,当你向它发送一个相同的用户名和密码时,它被设置为为 return 生成一个令牌,如果你给一个body 像这样

这是有效的,它给你一个令牌和正确的信息,但是当我从 chrome、firefox 或 ie 这样做时,它给我一个错误,我的代码是这样的,

var d = {
        username: Uname,
        surname: Pword,
        grant_type: "password",
        client_id: "099153c2625149bc8ecb3e85e03f0022",
    };
    console.log(d);

    var self = this;
    return (
        axios.post('http://localhost/hydraauth/oauth/token', {
            headers: {
                'Content-Type': 'application/json',
            },
            data: d
        }).then(function (response) {
            console.log(response.data)
            self.setState({
                isLoggedIn: true,
                Uname: Uname,
            }, function () {
                sessionStorage.setItem("Login", true);
                sessionStorage.setItem("Uname", Uname)
                window.location.href = "/";
            });
        }).catch(function (error) {
            if (error.response){
            console.log(error.response)
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
            } else {
            window.alert("Incorrect Username Or Password Or Unauthorized To Access This.")
            }
        })
    );

这在 chrome

中给了我这个错误

这里是展开的错误

chrome 中网络选项卡的图像,响应选项卡为空

Axios 在通过正文发送数据时出现问题,因此您需要使用 npm plugin query string

并像这样使用它

axios.post("http://localhost/hydraauth/oauth/token", querystring.stringify({ "username": Uname, "password": Pword, "grant_type": "password", "client_id": "eyJjbGllbnRfaWQiOiJTYXZ2eS1Dcm0tUmVhY3QtSnMtU3lzdGVtIn0" }))