Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Angular AJAX request is visible twice in chrome network tab

I just noticed that when I made Angular (1.4.8) AJAX POST request it is visible twice in chrome network tab (first (355B) as angular.js:10765 and second (812B) as other where first looks like request and second as response - only second contains response data). I made identical request by use of jQuery and it's appearing as a single request (812B).

CODE:

return function ( id ) {
  var deferred = $q.defer()
    , data = {
      id: id || null,
      range: tbDateRange.get( true )
    }
    ;

  /* TODO - REMOVE */
  $.ajax({
    method: 'POST',
    url: path,
    dataType: 'JSON',
    data: data
  });

  $http.post( path, data )
    .success( function ( data ) {
      /*...*/
      deferred.resolve( data );
    } )
    .error( function ( error ) {
      /*...*/
    } );
  return deferred.promise;
};

And network tab screenshot: enter image description here

like image 329
LJ Wadowski Avatar asked Jan 07 '16 10:01

LJ Wadowski


1 Answers

Angular defaults to POSTing JSON formatted data instead of form encoded data (jQuery does not so the statement I made identical request by use of jQuery is incorrect).

Cross-origin, JSON formatted, POST requests require a preflight OPTIONS request.

Presumably (because you haven't show any details of the requests besides the end of the URL they are going to), the first of those requests is that preflight OPTIONS request.

like image 121
Quentin Avatar answered Nov 14 '22 23:11

Quentin