简体   繁体   English

React Js Axios Post请求未从Web API接收正文

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

So I have a web api that is set up to generate a token for to return when you send it a username and password that are the same this works through postman if you give a body like so 因此,我有一个Web API,该API设置为生成令牌,以便在向其发送用户名和密码时返回该令牌,如果您提供这样的正文,则该名称和密码与邮递员的工作原理相同

在此处输入图片说明

this works it gives you a token and the correct information but when i do it from chrome, firefox or ie it gives me an error my code is this, 这有效,它为您提供了令牌和正确的信息,但是当我从chrome,firefox或其他方式执行操作时,它给了我一个错误,我的代码是这样,

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.")
            }
        })
    );

which gives me this error in chrome 这给了我这个镀铬的错误

在此处输入图片说明

here is the error expanded 这是错误扩大

在此处输入图片说明

image of the network tab in chrome the response tab is empty chrome中的网络标签的图片响应标签为空

在此处输入图片说明

Axios has an issue with sending data via body so you need to use npm plugin query string Axios在通过主体发送数据时遇到问题,因此您需要使用npm插件查询字符串

and use it like so 像这样使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM