Reuven
Reuven

Reputation: 93

AngularJS's $http.post() sends each time two AJAX requests, one without the data and one with

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.

Upvotes: 4

Views: 1730

Answers (1)

ishara gunathilaka
ishara gunathilaka

Reputation: 19

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.

Upvotes: 1

Related Questions