如何设置头文件到$ http.post方法AngularJS 1.3.5?

时间:2016-06-07 13:17:46

标签: javascript angularjs

我写了一个休息的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>

1 个答案:

答案 0 :(得分:3)

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(){...});