2014-10-07 2 views
2

나는 각도 전문가가 올바른 방향으로 나를 가리킬 수 있기를 바랍니다. 아래의 형식으로 공장을 사용하는 경우, 어떻게 있도록 POST의 헤더를 수정 않습니다AngularJS, 공장에서 http 헤더를 올바르게 수정하는 방법

'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'? 

코드를 :

var tableModule = angular.module('theApp', ['ui-rangeSlider','ngScrollbar']); 

tableModule.factory('Services', ['$http', 
function($http) { 
    return { 
     get : function($path, callback) { 
      return $http.get($path).success(callback);  
     }, 
     post : function($path, $data, callback) { 
      return $http.post($path, $data).success(callback); 
     } 
    }; 
}]); 

답변

2

이유는 단순히 $http.post 세 번째 설정 인수 ...

post: function(path, data) { 
    return $http.post(path, data, { 
     headers: { 
      'Content-type': 'application/x-www-form-urlencoded' 
     } 
    }); 
} 
+0

왜 콜백을 제거 않았다을 사용할 수 있습니까? – nodoze

+0

@nodoze 그냥 습관. 나는 약속 객체들과 함께 작업하는 것을 선호한다. 내 대답의 요점은 헤더를 전달하는 방법을 보여준 것이었다. – Phil

+1

나는 이것이 트릭을했다고 생각한다. Phil에게 감사한다. 그것은 큰 문제를 해결하지 못했지만 헤더가 전달 된 것으로 보입니다. 내 큰 질문은 여기에있다 : http://stackoverflow.com/questions/26226519/accessing-post-data-in-python-with-cgi-fieldstorage-using-angularjs – nodoze

2

내가이 이전처럼 뭔가를 시도했다.

$scope.update = function (user) { 
     $http({ 
      method: 'POST', 
      url: 'https://mytestserver.com/that/does/not/exists', 
      headers: { 
       'Content-Type': 'application/x-www-form-urlencoded' 
      }, 
      transformRequest: function (data) { 
       var postData = []; 
       for (var prop in data) 
       postData.push(encodeURIComponent(prop) + "=" + encodeURIComponent(data[prop])); 
       return postData.join("&"); 
      }, 
      data: user 
     }); 
    } 

은 또한 볼 같은 방법은 내 바이올린 보일 것이다 http://jsfiddle.net/cmyworld/doLhmgL6/

관련 문제