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

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

问题描述

在我的当前项目我有一个Drupal的后端,对我的前端暴露REST服务。
我的后端部分电话真的不喜欢的网址实体获得EN codeD。

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

有什么建议?谢谢!

Any suggestions? Thanks!

推荐答案

更新:新的 httpParamSerializer 在角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;
}

连接codeUriQuery 使用标准的连接codeUriComponent (<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/en$c$cURIComponent\"相对=nofollow> MDN )取代了用%2B+

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

太糟糕了,你不能覆盖连接codeUriQuery ,因为它是角函数中的局部变量。

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

所以我看到的唯一选择是覆盖 window.en codeURIComponent 。我已经做到了在$ 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天全站免登陆