Botnary / wp-slim-framework

Slim framework as Wordpress plugin
73 stars 26 forks source link

Ajax CORS #7

Closed bonzysalesman closed 8 years ago

bonzysalesman commented 8 years ago

I'm getting the following error message when trying to access my API, which I created using wp-slim-framework: "XMLHttpRequest cannot load http://myafrigrid.com/slim/api/marker/12293-785. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8101' is therefore not allowed access."

How can I fix this?

I'm sort of a newbie.

Botnary commented 8 years ago

This happens because of origin policy, you are making ajax requests from http://localhost:8101 to http://myafrigrid.com/slim/api/marker/12293-785, but XMLHttpRequest must be from the same domain, to be able to get the response your way from http://myafrigrid.com/slim/api/marker/12293-785 you must include in the response header like this example $app->response->headers->get('Access-Control-Allow-Origin','*'); , this might be a security issue since you allow any domain this way, or read more about origin policy on google.