【问题标题】:JQuery encodeURIComponent page not found errorJQuery encodeURIComponent 页面未找到错误
【发布时间】:2013-12-21 05:02:09
【问题描述】:

我正在尝试使用encodeURIComponent 函数对我的uri 进行编码。这是我的代码。

//res[0].CLIENT_ID=10 and id=res[0].CLIENT_ID
var url = "new_quotation.php?clientid="+res[0].CLIENT_ID+"&quoteid="+id;
var encodedurl = encodeURIComponent(url);
$('#edit').attr("href", encodedurl);

它成功编码uri,但是当页面重定向时它显示错误

在此服务器上找不到请求的 URL /Quotation/new_quotation.php?clientid=10&quoteid=0000000014。

我看到了url。好像

http://localhost/Quotation/new_quotation.php%3Fclientid%3D10%26quoteid%3D0000000014

那么,uri 被编码了,但是为什么页面没有被重定向呢?我需要使用任何其他功能来重定向吗?还是我的代码有错误?

【问题讨论】:

  • 没有理由对整个事物进行编码
  • @epascarello 你是什么意思?
  • 使用encodeURIComponent(res[0].CLIENT_ID)encodeURIComponent(id)而不是对整个url进行编码

标签: javascript php jquery url encoding


【解决方案1】:

您应该只对值进行编码,而不是对整个 url。

var url = "new_quotation.php?clientid="+encodeURIComponent(res[0].CLIENT_ID)+"&quoteid="+encodeURIComponent(id);
$('#edit').attr("href", url);

由于你使用的是jQuery,你也可以使用param()

【讨论】:

  • 因为它是这样工作的......它不知道 url 并且只对部分进行编码。你需要这样做。
  • 如果我使用 encodeURIComponent(res[0].CLIENT_ID) 它不会编码任何东西
  • 是特殊字符吗?
  • 显示 url 为http://localhost/Quotation/new_quotation.php?clientid=10&quoteid=0000000014 与原始相同
  • 这是 100% 有效的。现在输入一个空格,&, ?或者是其他东西。它将对其进行编码。 console.log(encodeURIComponent("10%!"));
【解决方案2】:

URL 中的每个参数都必须应用 encodeURIComponent(如果参数由特殊字符组成)

例子:

var enc =param1+'/'+encodeURIComponent(param2)+'/'+encodeURIComponent(param3);

param2, param3 - 这里应该有特殊字符

【讨论】:

    猜你喜欢
    • 2015-06-22
    • 2012-09-15
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2016-01-18
    相关资源
    最近更新 更多