0

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.

access-control-allow-origin

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 :).

1
  • 1
    This is the most significant bit of the error message: The response had HTTP status code 404 Commented Sep 20, 2016 at 13:52

1 Answer 1

2

SOLVED IT! finally... :D

based on this link, finally I fixed the problem..

Thanks...

Sign up to request clarification or add additional context in comments.

Comments

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.