首页 > 解决方案 > React 在 React 中找不到 url

问题描述

您好正在表单提交上执行 LoginValidation。我在提交时调用方法 validatelogin。方法如下,

ValidateLogin(e)
{
    if(this.state.formValid)
    {
        let userid = this.state.userId;
        let password = this.state.password;
         let url = "http://localhost:3727/api/Login/ValidateUser";
            fetch(url, {
            method: "POST",
            mode: 'no-cors',
            body : "userid=" + userid +"&password=" +  password
            }).then((response) => console.Log(respose));
    }
};

使用代码执行它显示 url not found return 404 但邮递员正在使用相同的 url。此网址仅接受发布请求。没有得到任何线索是怎么回事。我们是否必须在 React 中注册一些端点才能进行 api 调用?

标签: javascript

解决方案


试试这个。它正在处理我的情况。请确保客户端和服务在同一个域和同一个端口中(cors 不是问题)。

ValidateLogin(e)
{
    if(this.state.formValid)
    {
        let userid = this.state.userId;
        let password = this.state.password;
         let url = "http://localhost:3727/api/Login/ValidateUser";
            fetch(url, {
            method: "POST",
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
            }),
            mode: 'no-cors',
            body : "userid=" + userid +"&password=" +  password
            }).then((response) => console.Log(respose));
    }
};

推荐阅读