Reputation: 1216
So I have a project that I am creating with ReactJS. I am trying to Log In, and this is my React Code:
handleClick(event){
var apiBaseUrl = "http://127.0.0.1:8000/api/auth/login";
var self = this;
var payload={
"email":this.state.email,
"password":this.state.password
}
axios.post(apiBaseUrl, payload)
.then(function (response) {
console.log(response);
if(response.data.code == 200){
console.log("Login successfull");
var uploadScreen=[];
uploadScreen.push(<UploadScreen appContext={self.props.appContext}/>)
self.props.appContext.setState({loginPage:[],uploadScreen:uploadScreen})
}
else if(response.data.code == 204){
console.log("Username password do not match");
alert("username password do not match")
}
else{
console.log("Username does not exists");
alert("Username does not exist");
}
})
.catch(function (error) {
console.log(error);
});
}
I am not quite familiar with React and I just got this code here Just trying it out before I edit it. I am trying to access my API which I created with Laravel 5.3 but I get the error:
Failed to load http://127.0.0.1:8000/api/auth/login: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87)
I have searched for answers and they said that I should use CORS. I have Laravel CORS.
my cors.php
in config
'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedHeaders' => ['*'],
'allowedMethods' => ['*'],
'exposedHeaders' => [],
'maxAge' => 0,
'hosts' => [],
Upvotes: 0
Views: 3106
Reputation: 189
need to stringify the payload.
var payload = JSON.stringify({yourdata: yourdata});
and also try to add header like this.
var config = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
withCredentials: false
}
axios.post(apiBaseUrl, payload, config)
Upvotes: 4