0

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' => [],
2
  • try this github.com/barryvdh/laravel-cors Commented Sep 8, 2018 at 7:39
  • I am using that @rkj Commented Sep 8, 2018 at 7:49

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

2 Comments

i update the answers, try to add stringify at payload variable.
no need to update, your previous answer worked when I installed the Allow-Control-Allow-Origin:* Extension for Chrome! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.