Reputation: 1201
I am finding it difficult to send http post request to laravel api. GET method is just working fine but I get below error in google chrome console
Failed to load http://localhost:8000/api/user/auth: 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:8100' is therefore not allowed access.
It is been 2 days since i am searching for solution. I found a post on ionic official blog here https://blog.ionicframework.com/handling-cors-issues-in-ionic/ but it doesn't seem to help.
Thanks
Here is my code
authUser(email, pass) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
let data = JSON.stringify(
{
"email": email,
"password": pass
}
);
this.http.post(this.API_URL+'user/auth', data, httpOptions).subscribe(data => {
if(data['success']) {
this.user = data['response'];
} else {
this.hasError = data['message'];
console.log(this.hasError);
}
});
}
Upvotes: 2
Views: 591
Reputation: 1201
Well I found the solution I have changed Content-Type: application/json and the data body, below is the example
authUser(email, pass) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded' //updated
})
};
let data = "email="+email+"&password="+pass+""; //updated
this.http.post(this.API_URL+'user/auth', data, httpOptions).subscribe(data => {
if(data['success']) {
this.user = data['response'];
console.log(this.user);
} else {
this.hasError = data['message'];
console.log(this.hasError);
}
});
}
Upvotes: 3