1

I am using $http to post data to server.

There are two OPTIONS requestMethod calls before making a POST request.

However, the POST request is not made sometimes.

As I am updating html on return of this call, the page hangs.

 $http({
       url: scope.Settings.url,
               method: "POST",
                data:data,
                withCredentials: true
            }).then(function (data, status, headers, config) {
                setBusyState(false);
                scope.rModel = scope.search;
                scope.Results = data.data;
            }, function (data, status, headers, config) {
                scope.Results = [];
                setBusyState(false);
            });

EDIT: This does not happen always. Only a few times.

This appears to happen only in Chrome, whereas it is fine in IE

13
  • Have you checked for any console errors and monitored the network tab of chrome browser or tried to see what fiddler is reporting?If so share those updates. Commented Jun 27, 2016 at 8:24
  • 1
    Also angularjs caches the request in IE so always use app.config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push('requestInterceptor'); $httpProvider.defaults.cache = false; if (!$httpProvider.defaults.headers.get) { $httpProvider.defaults.headers.get = {}; } $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; $httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; } ]); Commented Jun 27, 2016 at 8:27
  • Looks like CORS issue. Have you checked the console? Commented Jun 27, 2016 at 8:28
  • Also make sure your endpoint is responding. This can give the impression that the req is not being made. Commented Jun 27, 2016 at 8:28
  • I checked Fiddler. It shows me only two OPTIONS request. No POST request is made. Also no console errors appear Commented Jun 27, 2016 at 8:34

2 Answers 2

1

This is a known issue with AngularJS where it caches the subsequent http requests so if you have multiple http requests lined up you would sometime experience that some of the calls are not actually made.I have mainly seen this issue of aggressive caching in IE.

To overcome this issue you need to apply some sort of global settings which will prevent the http requests from getting cached.The ideal place of doing this in angular is $httpProvider.

Basically you are setting different header parameters with the appropriate values.

Related code from app.config

appConfig.config(['$httpProvider', function ($httpProvider) {       
    $httpProvider.defaults.cache = false;
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};
    }
    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = <set a time>;
    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}
]);
Sign up to request clarification or add additional context in comments.

Comments

0

What angular does in chrome is, it makes OPTIONS request.

These Pre-Flight OPTIONS requests are a way of asking permissions for the actual request, before making the actual request.

So, as a work around, adding headers to the request solves the problem.

$http({
    url: scope.Settings.url,
    method: "POST",
    data: data,
    headers: {
        'Content-Type': 'application/json'
    },
    withCredentials: true
}).then(function(data, status, headers, config) {
    setBusyState(false);
    scope.rModel = scope.search;
    scope.Results = data.data;
}, function(data, status, headers, config) {
    scope.Results = [];
    setBusyState(false);
});

Comments

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.