bryan bryan - 1 year ago 150
AngularJS Question

Angularjs getting OPTIONS and POST request

I am sending a POST request to a different domain like so in angular:

$'', { email: $, password: $scope.password })

And in my request logs, it was only sending an OPTION request.

So I added this to my

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT');

But now, I am getting an OPTION request first, and then a POST request. So because my OPTION request is first, I think it is still messing with my results.

Does anyone know how to only get the POST request and not the OPTIONS request?

Answer Source

You can prevent a preflight on POST by triggering a simple request

To achieve this, use $httpParamSerializerJQLike to encode your data (make sure to inject the serializer) and set the content-type to application/x-www-form-urlencoded:

  method: 'POST',
  data: $httpParamSerializerJQLike(YOUR_DATA),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download