I would like to specify dataType: 'json'
as in conventional jQuery $.ajax
. Is this possible with Angular.js $http.post
?
我想指定dataType:“json”,就像传统的jQuery $.ajax。这对角有可能吗?js美元http。职位?
3 个解决方案
#1
5
From http://docs.angularjs.org/api/ng.$http
从http://docs.angularjs.org/api/ng http美元
Transforming Requests and Responses Both requests and responses can be transformed using transform functions. By default, Angular applies these transformations:
可以使用转换函数对请求和响应进行转换。在默认情况下,角度应用这些变换:
Request transformations:
请求的转换:
if the data property of the request config object contains an object, serialize it into JSON format. Response transformations:
如果请求配置对象的数据属性包含对象,则将其序列化为JSON格式。响应的转换:
if XSRF prefix is detected, strip it (see Security Considerations section below)
如果检测到XSRF前缀,则去掉它(参见下面的安全注意事项部分)
- if json response is detected, deserialize it using a JSON parser
- 如果检测到json响应,则使用json解析器对其进行反序列化
So no need to set a data type it is done automatically
因此不需要设置自动完成的数据类型
#2
19
You can use the HTTP Config object to set the headers:
可以使用HTTP Config对象设置头文件:
$http({
method: 'POST',
url: 'somewhere.xyz',
headers: {
'Content-type': 'application/json'
}
})
#3
3
I had the same problem, responseType:'json'
solved the issue
我遇到了同样的问题,responseType:'json'解决了这个问题
You can use responseType:'json' instead of dataType:'json'
可以使用responseType:'json'而不是dataType:'json'
var promise = $http({
method: 'POST',
url: 'somewhere.xyz',
responseType:'json'
});
For further reference https://docs.angularjs.org/api/ng/service/$http#methods_jsonp
为进一步参考https://docs.angularjs.org/api/ng/service/ http # methods_jsonp美元
#1
5
From http://docs.angularjs.org/api/ng.$http
从http://docs.angularjs.org/api/ng http美元
Transforming Requests and Responses Both requests and responses can be transformed using transform functions. By default, Angular applies these transformations:
可以使用转换函数对请求和响应进行转换。在默认情况下,角度应用这些变换:
Request transformations:
请求的转换:
if the data property of the request config object contains an object, serialize it into JSON format. Response transformations:
如果请求配置对象的数据属性包含对象,则将其序列化为JSON格式。响应的转换:
if XSRF prefix is detected, strip it (see Security Considerations section below)
如果检测到XSRF前缀,则去掉它(参见下面的安全注意事项部分)
- if json response is detected, deserialize it using a JSON parser
- 如果检测到json响应,则使用json解析器对其进行反序列化
So no need to set a data type it is done automatically
因此不需要设置自动完成的数据类型
#2
19
You can use the HTTP Config object to set the headers:
可以使用HTTP Config对象设置头文件:
$http({
method: 'POST',
url: 'somewhere.xyz',
headers: {
'Content-type': 'application/json'
}
})
#3
3
I had the same problem, responseType:'json'
solved the issue
我遇到了同样的问题,responseType:'json'解决了这个问题
You can use responseType:'json' instead of dataType:'json'
可以使用responseType:'json'而不是dataType:'json'
var promise = $http({
method: 'POST',
url: 'somewhere.xyz',
responseType:'json'
});
For further reference https://docs.angularjs.org/api/ng/service/$http#methods_jsonp
为进一步参考https://docs.angularjs.org/api/ng/service/ http # methods_jsonp美元