Hello
I have having some problems sending data to a server using the http.post method. Whenever i try to send the data i get the following error which i know to be cause to Same Origin Policy.
XMLHttpRequest cannot load http://energy-audit.22web.org/PHP/view.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://169.254.61.33:8100' is therefore not allowed access. The response had HTTP status code 403.
post(url, data, [config]);
When i remove the data parameter i get the response from the server but when it back i get the error above. The http get works fine but i need to get the post working so i can query item from my database.
On the server PHP i have the following CORS headers
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
but still get the error mentioned above…
IS there anyway to solve this issue…
So i was able to solve my issue by reading this post which stated the following
The problem you may encounter is that your server does not appear to receive the { foo: ‘bar’ } params from the AngularJS request.
The difference is in how jQuery and AngularJS serialize and transmit the data. Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively—that’s a darn shame because AngularJS is certainly not doing anything wrong. By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { “foo”: “bar”, “baz”: “moe” } JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.
The site also suggested the following be added
// Your app’s root module…
angular.module(‘MyModule’, , function($httpProvider) {
// Use x-www-form-urlencoded Content-Type
$httpProvider.defaults.headers.post[‘Content-Type’] = ‘application/x-www-form-urlencoded;charset=utf-8’;