4

I have a weird problem with AngularJS's $http service which, as far as I can see, no one else has.

Every time I use $http.post() to sent cross domain requests, I can see in the developer tools (network pane) that two different requests are being sent: one, without any data, and immediately after that another one is sent which has all the data and returns with the correct response from the server.

Here's an example:

$http.post(url+'getSiteList.php', {session_id: $scope.session_id(), withCredentials: true})
        .success(function(data, status, headers, config) {
            console.log(data);
....

Does anyone know what's causing this? I checked the code, and the $http.post method is only being called once.

Reuven
  • 93
  • 1
  • 5
  • 2
    possible duplicate of [AngularJS performs an OPTIONS HTTP request for a cross-origin resource](http://stackoverflow.com/questions/12111936/angularjs-performs-an-options-http-request-for-a-cross-origin-resource) – raina77ow Aug 30 '15 at 12:41

1 Answers1

1

It's all about how browsers manage CORS. When making a cross-domain request in angular, the browser will automatically make a HTTP OPTIONS request to the specified URL/URI it is called as "pre-flight" request or "promise".

As long as the remote source returns a HTTP status code of 200 and relevant details about what it will accept in the response headers, then the browser will go ahead with the original JavaScript call.