Reputation: 37
I wrote simple code with angularjs to communicate to PHP server API that I hosted in shared server before. I used $http.get and $http.post to get data from the server but it returned error 'access-control-allow-origin' like below:
http://dpmp.arkoding.tk/index.php/api/datapost. 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.
The response had HTTP status code 404.
most of the references that I got explained that it caused by CORS enabled on server site and just simply add a line 'header('Access-Control-Allow-Origin: *');' at the top for fix it. I have done that but it just fixed '$http.get' function and I still got the same error for '$http.post'. And made me more confused because it worked if I used postman and I thought the problem was in my angular code.
I have tried add some code in my services.js shown on the snippets below:
var datac = {id: 'helo'};
var head = {'Content-Type': 'application/json'};
return $http({
url: url + '/datapost',
method: 'POST',
headers: head,
data: datac
});
and without header
var datac = {id: 'helo'};
return $http({
url: url + '/datapost',
method: 'POST',
data: datac
});
but it wouldn't work. I even have tried to move my rest API to another hosting provider and it also returned the same error when I used angular $http.post. I'm very appreciated for your help to my problem, and I am sorry for my untidy english by the way :).
Upvotes: 0
Views: 1624