javascript - AngularJS : request PUT send a request OPTIONS before a request PUT -


when send post request, it's ok. when send put request (replace $http.post() $http.put()) angular send options request without datas, wait response , send put request datas.

it's not corps problem because it's client send 2 requests.

for options request, json response isn't parsed because angular doesn't go success function.

i want angular not send options request. know problem ? know fix ?

the code :

var app = angular.module('myapp', []); app.config(function($httpprovider) {     delete $httpprovider.defaults.headers.common['x-requested-with'];     $httpprovider.defaults.headers.post['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';     $httpprovider.defaults.headers.put['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';     $httpprovider.defaults.usexdomain = true;     //     var param = function(obj) {         var query = '', name, value, fullsubname, subname, subvalue, innerobj, i;         for(name in obj) {             value = obj[name];             if(value instanceof array) {                 for(i=0; i<value.length; ++i) {                     subvalue = value[i];                     fullsubname = name + '[' + + ']';                     innerobj = {};                     innerobj[fullsubname] = subvalue;                     query += param(innerobj) + '&';                 }             }             else if(value instanceof object) {                 for(subname in value) {                     subvalue = value[subname];                     fullsubname = name + '[' + subname + ']';                     innerobj = {};                     innerobj[fullsubname] = subvalue;                     query += param(innerobj) + '&';                 }             }             else if(value !== undefined && value !== null) {                 query += encodeuricomponent(name) + '=' + encodeuricomponent(value) + '&';             }         }         return query.length ? query.substr(0, query.length - 1) : query;     };     // override $http service's default transformrequest (json application/x-www-form-urlencoded)     $httpprovider.defaults.transformrequest = [function(data) {         return angular.isobject(data) && string(data) !== '[object file]' ? param(data) : data;     }]; }); app.controller('login', function ($scope, $http) {     $scope.run = function() {         var file_data = $("#file").prop("files")[0];         var datas = {email:"email@domain.com", pass:sha1("xxxxxx")};         $http.put("http://myapi.com/user/connect", datas             ).success(function(data, status, headers, config) {                 console.log(data);             }).error(function(data, status, headers, config) {              });         return;     } }); 

the first request :

general remote address:127.0.0.1:80 request url:http://api.wezit.local/user/connect request method:options status code:200 ok  response headers access-control-allow-methods:post, get, put, delete access-control-allow-origin:* cache-control:no-cache, must-revalidate connection:keep-alive content-length:170 content-type:application/json; date:fri, 17 jul 2015 16:31:15 gmt expires:mon, 26 jul 1997 05:00:00 gmt keep-alive:timeout=5, max=100 pragma:no-cache server:apache/2.4.10 (fedora) php/5.5.25 set-cookie:phpsessid=2dj440b2vr2emi5ht9ojcl8gk6; path=/ set-cookie:phpsessid=q3pg80qb43ps6tpkljlvelo0k7; path=/ x-powered-by:php/5.5.25  request headers accept:*/* accept-encoding:gzip, deflate, sdch accept-language:fr-fr,fr;q=0.8,en-us;q=0.6,en;q=0.4 access-control-request-headers:accept, content-type access-control-request-method:put connection:keep-alive host:api.wezit.local origin:http://test.local referer:http://test.local/api.php user-agent:mozilla/5.0 (x11; linux i686 (x86_64)) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.125 safari/537.36 

the second request :

general remote address:127.0.0.1:80 request url:http://api.wezit.local/user/connect request method:put status code:200 ok  response headers access-control-allow-methods:post, get, put, delete access-control-allow-origin:* cache-control:no-cache, must-revalidate connection:keep-alive content-length:327 content-type:application/json; date:fri, 17 jul 2015 16:31:15 gmt expires:mon, 26 jul 1997 05:00:00 gmt keep-alive:timeout=5, max=99 pragma:no-cache server:apache/2.4.10 (fedora) php/5.5.25 set-cookie:phpsessid=18jfhgq2fs1p1f1nu7ua1ap8c3; path=/ set-cookie:phpsessid=14aifglpntf8amavkipclvom67; path=/ x-powered-by:php/5.5.25  request headers accept:application/json, text/plain, */* accept-encoding:gzip, deflate, sdch accept-language:fr-fr,fr;q=0.8,en-us;q=0.6,en;q=0.4 connection:keep-alive content-length:142 content-type:application/x-www-form-urlencoded; charset=utf-8 host:api.wezit.local origin:http://test.local referer:http://test.local/api.php user-agent:mozilla/5.0 (x11; linux i686 (x86_64)) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.125 safari/537.36  form data email:email@domain.com pass:ce35927f4dcb044bceda5f385823419cb0156507 

browsers make pre-flight request option method, when initiate cross-origin request. means api trying access on different origin application. there's nothing can this.

do know problem?

there no problem in observed, expected behaviour.

when send post request, it's ok.

here's reason why it's ok:

in particular, request preflighted if:

  • it uses methods other get, head or post. also, if post used send request data content-type other application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if post request sends xml payload server using application/xml or text/xml, request preflighted.
  • it sets custom headers in request (e.g. request uses header such x-pingother)

Comments