javascript - "%"正在进行 URI 解码,而其他所有内容都没有

标签 javascript uri sapui5 uriencoding

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

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

仅供引用,解码后的结果如下:Entity('Element:Info,ID')

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

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

因此,整个(非常基本的)“导航到” block 如下所示:

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);
},

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

Console output of console.log(oItem.getBindingContext().getPath().substr(1)): Entity('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 模式不是“%”,它就保持不变。 由于某种奇怪的原因,这个特殊字符被编码,而其他一切都不重要。

最佳答案

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

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

初始字符串:元素:信息,ID

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

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

因此,如果您从 URL 获取绑定(bind)上下文,则需要解码两次。 否则,正如您所做的那样,就可以了。

关于javascript - "%"正在进行 URI 解码,而其他所有内容都没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522784/

相关文章:

c# - Uri.TryCreate 对任何字符串值返回 true?

ios - 如何定义以 http ios7 开头的 url 方案

javascript - THREE.js 不使用 .mtl 渲染 .obj - 为 THREE.js 导出文件

php - 通过 POST 或 GET 发送大量数据

javascript - 捕获 ES6 promise 中的错误

javascript (node.js) 地理计算导致 NaN

android - 如何从项目资源android中解析一个xml

javascript - 在带有 VBox-SAP UI5 的 Javascript View 中使用 "href"

javascript - 数据绑定(bind)在 XML View 中不起作用

javascript - SAPUI5控件不显示