reactjs - 被 CORS 策略阻止:“Access-Control-Allow-Origin”
问题描述
我正在axios
使用 API 从 ReactJS 发送数据,但出现以下错误:
被 CORS 策略阻止:“Access-Control-Allow-Origin”
以下是错误:
从源“ http://localhost:3000 ”访问“ http://localhost/2019/eurdu/User_Controllers/UserRegisterController/userRegistration ”处的 XMLHttpRequest已被 CORS 策略阻止:请求标头字段 access-control-allow-origin 为Access-Control-Allow-Headers 在预检响应中不允许。const API_PATH_Signup = ' http://localhost/2019/eurdu/User_Controllers/UserRegisterController/userRegistration ';
下面是注册组件:
class SignUp extends Component{
constructor(props){
super(props);
this.state = {user_name:"",user_email:"",user_mobile:"",user_password:""}
}
handleFormSubmitSignup = e => {
e.preventDefault();
console.log(this.state);
axios({
method: 'post',
url: `${API_PATH_Signup}`,
headers: { 'content-type': 'application/json' ,
"access-control-allow-origin" : "*"
},
data: this.state
})
.then(result => {
console.log(result);
this.setState({
mailSent: result.data.sent
})
})
.catch(error => this.setState({ error: error.message }));
};
下面是 JSX:
<div className="col-sm-6 col-md-4 col-md-offset-4">
<h1 className="text-center login-title">Sign Up to EUrdu</h1>
<div className="account-wall">
<img className="profile-img" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=120"
alt="progimg"/>
<form id="myform">
<input type="text" className="form-control" placeholder="FirstName" name="user_name" id="user_name"
value={this.state.user_name}
onChange={e => this.setState({ user_name: e.target.value })}
/><br/>
<input type="text" className="form-control" placeholder="Email Id" id="user_email" name="user_email"
value={this.state.user_email}
onChange={e => this.setState({ user_email: e.target.value })}
/><br/>
<input type="text" className="form-control" placeholder="Phone Number" name="user_mobile" id="user_mobile"
value={this.state.user_mobile}
onChange={e => this.setState({ user_mobile: e.target.value })}
/><br/>
<input type="password" className="form-control" placeholder="Password" id="user_password" name="user_password"
value={this.state.user_password}
onChange={e => this.setState({ user_password: e.target.value })}
/><br/>
<button className="btn btn-lg btn-primary btn-block" type="submit"
onClick={e => this.handleFormSubmitSignup(e)} >
Sign in</button>
</form>
<span style={{textalign:'center'}}>Have an account?</span>
</div>
</div>
下面是 API 我也在标题中包含了这一行:
header('Access-Control-Allow-Origin: *');
下面是UserRegistration
函数:
function userRegistration()
{
header('Access-Control-Allow-Origin: *');
$name= $this->input->post('user_name');
$email= $this->input->post('user_email');
$phonenumber= $this->input->post('user_mobile');
$password= $this->input->post('user_password');
$data= array('user_name' => $name ,
'user_email' => $email ,
'user_mobile' => $phonenumber ,
'user_password' => $password ,
);
print_r($data);
exit();
$this->load->model('User/UserRegisterModel','userregistermodel');
$res= $this->userregistermodel->userRegistration($data);
$formData = array();
if($res){
$formData = array('status' => "true",'message'=>'successfully created' );
}else{
$formData = array('status' => "false",'message'=>'Please try again' );
}
echo json_encode($formData);
}
解决方案
如果在 localhostheader('Access-Control-Allow-Origin: *');
中创建应用程序不起作用。在您的浏览器中安装CORS Everywhere plugin
,然后检查它的响应是否正常......
插件网址:
For Firefox : https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/
For Chrome : https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
推荐阅读
- bash - 使用 xargs 将数组作为命令行参数传递
- blockchain - 部署实现 IUniswapV2Router02 的智能合约时出错
- geoserver - GeoServer中栅格查询上的多边形
- python - 无法在循环单链表中插入节点
- longhorn - 什么是 Longhorn 节点选择器?
- android - 使用 react-native 读取 XLSX 或 CSV 数据
- ruby - Ruby中名为“Proc”的对象的“全名”是什么
- python - 我想将 Outlook 中附加的 csv 文件作为数据框读入我的 Jupyter Notebook
- react-native - 如何将谷歌个人资料图片发送到服务器?
- hexagonal-architecture - 端口和适配器——接口是否应该将参数指定为域对象?