我可以在前端做任何事情来读取POST请求对另一个域的响应吗?

时间:2018-03-08 16:19:05

标签: cross-domain

我有一张表格

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        ...
      </form>
    );
  }

在提交时,我发帖请求并打印回复

  handleSubmit(event) {
    axios.post('https://.../oauth2/token', {
      firstName: this.state.username,
      password: this.state.password,
      grant_type: password
    })
    .then(response => console.log(response))

    event.preventDefault();
  }

打印的回复是

  

无法加载https://.../oauth2/token:响应   预检请求未通过访问控制检查:否   请求中存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:3000”   访问。响应的HTTP状态代码为400。

我知道这可以防止cookie滥用。但是我可以在前端做些什么来获得响应吗?也许我可以在请求中声明我不想使用任何cookie权限。

1 个答案:

答案 0 :(得分:1)

No&#39; Access-Control-Allow-Origin&#39;标头出现在请求的资源上

在那里告诉我什么是错的。您遇到CORS(跨源资源共享)问题。如果您正在使用Chrome,则可以使用extension

对于永久性解决方案,您必须在主机服务器上启用CORS。我无法提供太多帮助,因为我不知道你的后端是什么样的。您可以尝试搜索&#34; CORS&#34 ;.就个人而言,我只是在为了简单起见而开发时才使用扩展程序,但我只是在使用AngularJS移动应用程序。