angularjs - 在 AngularJS 中转换请求和响应

标签 angularjs http

转换在 angularjs http 服务中做了什么。以下是文档中如何解释自定义转换的示例。

Both requests and responses can be transformed using transformation functions: transformRequest and transformResponse. These properties can be a single function that returns the transformed value (function(data, headersGetter, status)) or an array of such transformation functions, which allows you to push or unshift a new transformation function into the transformation chain

function appendTransform(defaults, transform) {

// We can't guarantee that the default transformation is an array
  defaults = angular.isArray(defaults) ? defaults : [defaults];

  // Append the new transformation to the defaults
  return defaults.concat(transform);
}

$http({
  url: '...',
  method: 'GET',
  transformResponse: appendTransform($http.defaults.transformResponse, function(value) {
    return doTransform(value);
  })
});

什么变换会做?我无法从文档中得到解释,可以解释一下吗?

感谢您的帮助。

最佳答案

检查一下。这应该让您了解如何使用以及为什么它作为 $http 的服务之一存在

transforming your response using services.transformer.ApiResponse

因此您可以重新制定函数的 JSON.parse(data) 以获得您想要的响应!

关于angularjs - 在 AngularJS 中转换请求和响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563914/

相关文章:

angularjs - 当模型以编程方式更改时调用 ngChange

angularjs - Angular UI-Router 不显示模板

javascript - Angular ui-router动态地为所有状态添加解析功能

javascript - 根据收到的模型值加载不同模板的指令

java - Tomcat autodeploy 不通过 HTTP 返回实际文件

javascript - Angular 填充 ng-repeat 和 ng-model

java - 从 httpsrest api 调用 httprestapi 时出现错误 502

c++ - 简单的 Http 获取响应

linux - 如何查看服务器正在执行的所有请求 URL(最终 URL)

python - 为什么 aiohttp 不允许在 url 路径中使用冒号?