Javascript异常:encodeUri未定义

时间:2016-10-08 13:34:08

标签: javascript

现在我尝试在我的脚本中使用 encodeUri(),但在主题中收到异常。 传递给 encodeUri()的值有效,它发生在不同的(最新)浏览器中。

任何人都知道为什么它甚至可能会收到" undefined" encodeUri()的例外情况?

顺便说一下。它与 encodeUriComponent()相同。只有 escape()才会抛出异常。

非常感谢任何帮助

干杯, 汤姆

2 个答案:

答案 0 :(得分:0)

encodeURIComponent( string 返回" undefined"如果 string 参数是未定义

我建议你调试它并确保字符串有效。

你也可以尝试用另一个函数包装这个函数来添加手动验证:

function encode(string) {
      var stringIsInvalid = string === undefined ||
                            typeof string !== 'string' ||
                            string.length < 1;

      if(stringIsInvalid) throw new Error('Cannot encodeURIComponent(string),'+
                                          ' string is invalid');

      return encodeURIComponent(string);
 }

答案 1 :(得分:0)

要为其他人完成此主题,第二个解决方案是:始终注意正确输入:编码 URI ()