【问题标题】:"%" is getting URI decoded while everyhting else not“%”正在获取 URI 编码,而其他所有内容都没有
【发布时间】:2015-05-29 06:40:25
【问题描述】:

我有一个奇怪的 UI5 问题。我从控件的绑定上下文创建一个字符串,如下所示:

Entity('Element%3AInfo%2CID')

仅供参考,它看起来像这样解码:Entity('Element:Info,ID')

但是,我从以下方法链中获取此字符串:

oItem.getBindingContext().getPath().substr(1)

所以,整个(非常基本的)“导航到”块看起来像这样:

showElement : function (oItem) {
    'use strict';

    var bReplace = jQuery.device.is.phone ? false : true;

    sap.ui.core.UIComponent.getRouterFor(this).navTo("element", {
        from: "master",
        element: oItem.getBindingContext().getPath().substr(1),
        otherpattern: "something"
    }, bReplace);
},

此块中的控制台日志console.log(oItem.getBindingContext().getPath().substr(1)); 提供了正确的字符串。

console.log(oItem.getBindingContext().getPath().substr(1)) 的控制台输出: 实体('Element%3AInfo%2CID')

问题是(请注意,这很奇怪)我的 URL 模式“{element}”被填充了:

Entity('Element%253AInfo%252CID')

解码:Entity('Element%3AInfo%2CID')

您可能已经知道,模式的“%”是经过编码的。我不明白为什么 UI5 会这样做。

您还应该知道我测试过的这些事实:

  • decodeURIComponent(oItem.getBindingContext().getPath().substr(1)) 导致“Entity('Element:Info,ID')
  • encodeURIComponent(oItem.getBindingContext().getPath().substr(1)) 导致“Entity('Element%25253AInfo%25252CID')
  • oItem.getBindingContext().getPath().substr(1).replace("%3A", ":") 导致“Entity('Element:Info%252CID')

这是一个错误吗?我的意思是只要不出现“%”,URI 模式就不会受到影响。 出于某种奇怪的原因,这个特殊字符被编码,而其他一切都无关紧要。

【问题讨论】:

  • 我的直觉是这个库需要一个解码的字符串。
  • @royhowie 完全解码的字符串(请参阅第一个列表项)不适合,因为我需要像这样的字符串 Entity('Element%3AInfo%2CID')

标签: javascript uri sapui5 uriencoding


【解决方案1】:

它并不完全像“%”被编码而其他所有东西都没有被编码。

我也遇到过这个问题。 SAPUI5 编码一次,browser 编码第二次。因此,在第二次迭代中,您将只需要编码“%”。

初始字符串:Element:Info,ID

在第一次编码迭代后(通过 UI5 框架)encodeURIComponent('Element:Info,ID'):我们得到Element%3AInfo%2CID

所以对于第二次迭代,只剩下 % 被编码Element%253AInfo%252CID 因此我们得到了这个。

因此,如果您从 URL 中获取绑定上下文,则需要解码两次。 否则你做一次就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多