1

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.

2 Answers 2

2

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 * 
Sign up to request clarification or add additional context in comments.

6 Comments

I made it but I didn't help. I still have the same problem.
did your server responding with that header which I mentioned ?
In my action that sends response I have following code $response = new JsonResponse(); $response->headers->set('Access-Control-Allow-Origin','*'); return $response;
try to debug the request with chrome network and make sure what you sending without the X-Requested-With and your server responding with Access-Control-Allow-Origin *
I don't know why bu in Request I have this Request Method:OPTIONS Status Code:405 Method Not Allowed And response returns Allow:GET, HEAD, POST
|
1

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>

4 Comments

Should I add into my host that works with static files or that works with API?
I added this lines into my htaccess file tried different combinations and it still not works. It gonna kill me)
Instead of POST request it sends OPTIONS request. I made a2enmod headers, allowmethods for my Apache and I'm getting new error: XMLHttpRequest cannot load api.host.loc/app_dev.php/add. Invalid HTTP status code 405
is everything ok on the sf side? Your action allow the right method?

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.