madberdin
madberdin

Reputation: 47

Symfony 2.5 and Angular $http

I have some problem when I'm using $http with my API that I've written using Symfony. When I'm using $http.get at server side I'm adding a header to response : Access-Control-Allow-Origin and everything works and I can get required data from server. But when I'm using $http.post and adding this header , nothing works.

$http.post('http://myhost.loc/posts', {data:'Test string'}).success(function(data, status){
    console.log(data);
}).error(function(data, status){
    console.log(status);
});

And I get an error : OPTIONS http://myhost.loc/posts and No 'Access-Control-Allow-Origin' header is present on the requested resource. I don't understand why this doesn't work.

Upvotes: 1

Views: 1061

Answers (2)

thesearentthedroids
thesearentthedroids

Reputation: 620

Before sending a cross domain request, there is a preflight request sent with the OPTIONS method. The purpose of that preflight request is to check if the ressource is reachable

You can try that in your apache .htaccess

<ifModule mod_headers.c>
    Header always set Access-Control-Allow-Origin: "*"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "origin, x-requested-with, content-type"
</ifModule>

Upvotes: 1

Narek Mamikonyan
Narek Mamikonyan

Reputation: 4611

its a CORS problem first of all enable CORS on your angular app by

myApp.config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }
]);

A server supporting CORS must respond to requests with several access control headers:

• Access-Control-Allow-Origin * 

Upvotes: 2

Related Questions