AngularJS 资源:如何禁用 url 实体编码 [英] AngularJS resource: how to disable url entity encoding

查看:26
本文介绍了AngularJS 资源:如何禁用 url 实体编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我有一个 drupal 后端,它为我的前端公开休息服务.对我的后端的一些调用并不真正喜欢对 url 实体进行编码.

On my current project I have a drupal backend that exposes rest services for my frontend. Some calls to my backend don't really like url entities to get encoded.

所以我的问题是:如何禁用某些参数的 URL 编码?

So my question is: how do I disable URL encoding of some parameters?

示例:

我需要在不同的搜索词之间使用+"号来调用我的后端.像这样:

I need to call my backend with a "+"-sign between different search terms. Like so:

http://backend.com/someservice/search/?terms=search+terms+here

但是有角度的,设置如下:

But angular, setup like so:

var resource = $resource(
  backendUrl + '/views/:view', {},
    {
      'search': {params:{view:'searchposts'}, isArray:true}
    }
 );

// search posts for the given terms
this.searchPosts = function(terms, limit) {
  resource.search({search:terms.join('+'), limit:limit});
};

调用以下网址:

http://backend.com/someservice/search/?terms=search%2Bterms%2Bhere

有什么建议吗?谢谢!

推荐答案

更新:使用新的 httpParamSerializer 在 Angular 1.4 中,您可以通过编写自己的 paramSerializer 并设置 $httpProvider.defaults.paramSerializer 来实现.

Update: with the new httpParamSerializer in Angular 1.4 you can do it by writing your own paramSerializer and setting $httpProvider.defaults.paramSerializer.

以下仅适用于 AngularJS 1.3(及更早版本).

不改变AngularJS的源是不可能的.

It is not possible without changing the source of AngularJS.

这是由 $http 完成的:

This is done by $http:

https://github.com/angular/angular.js/tree/v1.3.0-rc.5/src/ng/http.js#L1057

function buildUrl(url, params) {
      if (!params) return url;
      var parts = [];
      forEachSorted(params, function(value, key) {
        if (value === null || isUndefined(value)) return;
        if (!isArray(value)) value = [value];

        forEach(value, function(v) {
          if (isObject(v)) {
            v = toJson(v);
          }
          parts.push(encodeUriQuery(key) + '=' +
                     encodeUriQuery(v));
        });
      });
      if(parts.length > 0) {
        url += ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
      }
      return url;
}

encodeUriQuery 使用标准的 encodeUriComponent (MDN) 将 '+' 替换为 '%2B'

encodeUriQuery uses the standard encodeUriComponent (MDN) which replaces the '+' with '%2B'

很遗憾你不能覆盖 encodeUriQuery 因为它是 angular 函数中的一个局部变量.

Too bad you cannot overwrite encodeUriQuery because it is a local variable inside the angular function.

所以我看到的唯一选择是覆盖 window.encodeURIComponent.我已经在 $http 拦截器中完成了它以最小化影响.请注意,原始函数仅在响应返回时才放回,因此在您的请求正在进行时,此更改是全局的 (!!).因此,请务必测试这是否不会破坏您应用程序中的其他内容.

So the only option I see is to overwrite window.encodeURIComponent. I've done it in an $http interceptor to minimize the impact. Note that the original function is only put back when the response comes back, so this change is global (!!) while your request is ongoing. So be sure to test if this doesn't break something else in your application.

app.config(function($httpProvider) {
  $httpProvider.interceptors.push(function($q) {
    var realEncodeURIComponent = window.encodeURIComponent;
    return {
      'request': function(config) {
         window.encodeURIComponent = function(input) {
           return realEncodeURIComponent(input).split("%2B").join("+"); 
         }; 
         return config || $q.when(config);
      },
      'response': function(config) {
         window.encodeURIComponent = realEncodeURIComponent;
         return config || $q.when(config);
      }
    };
  });
});

这篇关于AngularJS 资源:如何禁用 url 实体编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆