我尝试了以下命令,使用 Angular 上的资源:
angular.module('appServices', ['ngResource']).factory('Example',
function($resource){
return $resource('http://api.example.com.br/teste', {}, {
query: {method:'GET', headers: {'Content-Type': 'application/json'}}
});
});
但未正确生成 http 内容类型,在本例中为“application/json”。
我见过类似的问题,比如AngularJS 资源没有设置 Content-Type,但我有最新的 Angular 版本(1.0.6/1.1.4)。
上面的代码有什么问题?
结论
- 如下所述,HTTP Get 方法不应该有主体。
- 属性标头在上述版本中不起作用。我使用以下命令失败:查询:{method:'POST',标题:{'Content-Type':'application/json'}}
- 这种方式对我有用: $http.defaults.headers.put['Content-Type'] = 'application/json';