【问题标题】:How to set header to the $http.post method AngularJS 1.3.5?如何将标头设置为 $http.post 方法 AngularJS 1.3.5?
【发布时间】:2016-10-07 10:13:23
【问题描述】:

我写了一个rest webservice,以避免我在java代码中遇到的错误: 注册的消息正文阅读器与 MIME 媒体类型兼容,我必须在我的 $http.post 中添加一个 'Content-Type': 'application/x-www-form-urlencoded' 或 'Content-Type': 'application/ json'。 我正在使用 Angularjs 1.3.5,每当我尝试添加标题 {content-type.... } 时,我都失败了。我能做些什么来解决我的问题?

$scope.save = function(url,data) {
    var data_stack = $scope.stack;
		$http.post(url, data_stack)
            .then(
           function(response){
                }, 
           function(response){
                });
}
<form ng-submit="save()">
      <input ng-model="stack"></input>
      <button type="submit">Save</button>
</form>

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:
    var req = {
     method: 'POST',
     url: 'http://example.com',
     headers: {
       'Content-Type':'application/x-www-form-urlencoded' 
       // or  'Content-Type':'application/json'
     },
     data: { test: 'test' }
    }
    
    $http(req).then(function(){...}, function(){...});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多