简体   繁体   English

如何阻止Angular $ http反序列化响应正文?

[英]How can I stop Angular $http from deserializing the response body?

This: 这个:

$http({method: 'GET', url: '/some/url/returning/json').
        success(function(data, status, headers, config) {
            console.log(data);
        });

shows that Angular is giving me a JavaScript object as the first argument to my success handler. 显示Angular给我一个JavaScript对象作为success处理程序的第一个参数。 Presumably, it is trying to be smart by sniffing the content-type of the response. 大概是通过嗅探响应的内容类型来尝试变得聪明。

This is undesired behavior. 这是不希望的行为。 How do I tell Angular to give me the response body as a string? 如何告诉Angular将响应主体作为字符串给我?

The undesired behavior is caused by Angular's transformResponse . 不良行为是由Angular的transformResponse引起的。 To turn this off, do: 要关闭此功能,请执行以下操作:

$http({method: 'GET', url: '/some/url/returning/json', transformResponse: [] }).
        success(function(data, status, headers, config) {
            console.log(data);
        });

它在文档中提到可以使用responseType config选项,大概是这样的:

$http({method: 'GET', url: '/some/url/returning/json', responseType: 'text'})...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道$ http响应是否来自缓存 - Angular - How can I know if the $http response is brought from cache - Angular 失败时如何从Angular HTTP Interceptor访问Response主体? - How to access Response body from Angular HTTP Interceptor on failure? Angular:如何阻止$ http调用阻止UI? - Angular: How can I stop my $http calls from blocking the UI? 如何获取Angular $ http请求以返回响应? 我已经尝试过回调函数和Promise - How can I get my Angular $http request to return a response? I've tried callback functions and promises 如何在 Angular $http 中处理来自 Express response.write() 的响应 - How to handle response from Express response.write() in Angular $http 在继续angular.forEach循环之前,如何等待$ http响应 - How can I wait for $http response before continuing with angular.forEach loop 在Angular http响应拦截器中,如何获得实际的xhr对象? - In an Angular http response interceptor, how can I get at the actual xhr object? 为什么我不能使用Angular访问$ http错误响应标头? - Why can I not access $http error response headers with Angular? 从http响应正文下载文件 - Download a file from http response body 停止发送对angular http的响应并从控制器加载新的jsp页面 - stop sending response to angular http and loading new jsp page from controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM