简体   繁体   English

交叉原始请求仅支持协议方案中的错误响应

[英]Cross origin requests are only supported for protocol schemes error in react

when i tried to do post method in react with nodejs as backend , it is getting this error , i don't know why, but i think my coding is correct only. 当我试图做post方法与nodejs as backend反应,它得到这个error ,我不知道为什么,但我认为我的编码只是正确的。 please help me to solve this issue. 请帮我解决这个问题。

Failed to load localhost:3000/doctors/register: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
dispatchXhrRequest @ xhr.js:178
xhrAdapter @ xhr.js:12
dispatchRequest @ dispatchRequest.js:59
Register.js:36 Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)
    at dispatchXhrRequest (xhr.js:178)
    at new Promise (<anonymous>)
    at xhrAdapter (xhr.js:12)
    at dispatchRequest (dispatchRequest.js:59)

in react 反应过来

import React, { Component } from 'react'
import axios from 'axios';

class Register extends Component {
  constructor() {
    super()
    this.state = {
      name: '',
      gender:'',
      designation:'',
      email: '',
      password: '',
      confirm_password: '',
      hospital_id:'',
      errors: {}
  }
  this.onChange=this.onChange.bind(this);
  this.onSubmit=this.onSubmit.bind(this);
  }
  onChange(e) {
    this.setState({[e.target.name]:e.target.value})
  }
  onSubmit(e) {
    e.preventDefault();
    var resObj ={
      name:this.state.name,
      gender:this.state.gender,
      designation:this.state.designation,
      email:this.state.email,
      password:this.state.password,
      confirm_password:this.state.confirm_password,
      hospital_id:this.state.hospital_id
    }
    axios.post('localhost:3000/doctors/register',resObj)
    .then(res => console.log(res.data))
    .catch(err => console.log(err))
  }
  render() {
    return (
      <div className="register">
        <div className="container">
          <div className="row">
            <div className="col-md-8 m-auto">
              <h1 className="display-4 text-center">Sign Up</h1>
              <p className="lead text-center">Create your account</p>
              <form onSubmit={this.onSubmit}>
                <div className="form-group">
                  <input type="text" className="form-control form-control-lg" placeholder="Name" name="name" value={this.state.name} onChange={this.onChange} />
                </div>
                <div className="form-group">
                  <input type="text" className="form-control form-control-lg" placeholder="Gender" name="gender" value={this.state.gender} onChange={this.onChange} />
                </div>
                <div className="form-group">
                  <input type="text" className="form-control form-control-lg" placeholder="Designation" name="designation" value={this.state.designation} onChange={this.onChange} />
                </div>
                <div className="form-group">
                  <input type="email" className="form-control form-control-lg" placeholder="Email Address" name="email" value={this.state.email} onChange={this.onChange}/>
                </div>
                <div className="form-group">
                  <input type="password" className="form-control form-control-lg" placeholder="Password" name="password" value={this.state.password} onChange={this.onChange} />
                </div>
                <div className="form-group">
                  <input type="password" className="form-control form-control-lg" placeholder="confirm Password" name="confirm_password" value={this.state.password2} onChange={this.onChange} />
                </div>
                <div className="form-group">
                  <input type="text" className="form-control form-control-lg" placeholder="Hospital_id" name="hospital_id" value={this.state.hospital_id} onChange={this.onChange} />
                </div>
                <input type="submit" className="btn btn-info btn-block mt-4" />
              </form>
            </div>
          </div>
        </div>
      </div>
    )
  }
}

export default Register;

You missed the http:// or a simple // before localhost 您在localhost之前错过了http://或简单的//

Using //localhost... should automatically use the current used protocol 使用//localhost...应该自动使用当前使用的协议

Seperate answer as requested - once a proxy has been set up you not longer need to supply a full path and a relative one will work 根据要求单独回答 - 一旦设置了代理,您不再需要提供完整路径,并且相对的路径将起作用

localhost:3000/doctors/register to /doctors/register localhost:3000 /医生/注册/医生/注册

Solution that worked for me was to set headers to after defining app in the NodeJS server main/index/server.js file. 对我有用的解决方案是在NodeJS服务器main / index / server.js文件中定义app之后设置标题。

    app.use((req, res, next) => {
     res.set({
       'Access-Control-Allow-Origin': '*',
       'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
       'Access-Control-Allow-Headers': 'Content-Type'
     })
     next();
   })

This should fix it. 这应该解决它。

This is a CORS issue - put a proxy to your API inside your react client package.json 这是一个CORS问题 - 在您的react客户端package.json中为您的API提供代理

 "proxy": "http://localhost:5000/"

You will also then need to update your URL in your component 然后,您还需要更新组件中的URL

Heres a similar post How to create a proxy in React/Webpack to call an external API 下面是一篇类似的帖子如何在React / Webpack中创建一个代理来调用外部API

A project with REACT and Node CORS issue is solved as 解决了REACT和Node CORS问题的项目

React: 阵营:

axios.defaults.headers.post['Content-Type'] ='application/x-www-form-urlencoded';
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
axios.post('http://localhost:port/..../add_', obj)
    .then(res => console.log(res.data));

Here 'HTTP://.....' is mandatory. 这里'HTTP:// .....'是强制性的。 Node:app.js 节点:app.js

app.use(function(req,res,next){
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    res.header("Access-Control-Allow-Credentials", true);
    next();
});

暂无
暂无

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

相关问题 XMLHttpRequest仅服务器的协议方案支持跨源请求 - XMLHttpRequest Cross origin requests are only supported for protocol schemes in turn server Angular2中的协议方案问题仅支持跨源请求 - Cross origin requests are only supported for protocol schemes issue in Angular2 跨源请求仅支持协议方案:http - Cross origin requests are only supported for protocol schemes: http 本地主机-仅协议方案支持跨源请求? - localhost - Cross origin requests are only supported for protocol schemes? 仅协议方案支持React Native WebView Cross起源请求:http,data,chrome,https - React Native WebView Cross origin requests are only supported for protocol schemes: http, data, chrome, https AngularJS错误:仅支持协议方案的跨源请求:http,数据,chrome扩展,https - AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https 使用角度路由时出现错误仅协议方案仅支持跨源请求:http - Getting Error while using angular routing Cross origin requests are only supported for protocol schemes: http 为什么我的catch块无法捕获“协议方案仅支持跨源请求:http…” - Why Is My catch Block Not Catching “Cross origin requests are only supported for protocol schemes: http…” 交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https - Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https 无法加载“ username:password http://链接:仅协议方案http,数据,chrome,chrome扩展名支持跨源请求 - failed to load "username:password http:// link: Cross origin requests are only supported for protocol schemes http, data, chrome, chrome-extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM