0

encodeURIComponent()我有一个从 ajax 接收 url 的 php 页面,该 url在使用 ajax 发布之前正在经历。

在php页面中,我尝试了有urldecode()或没有它。两种方式都返回相同的错误网址,其中包含&而不是 & 我想我可以str_replace,但我认为这里还有其他问题,替换可能不是答案。

html/ajax 代码:

var ajax_url = $('#base_url').val() + 'xxx';
var url_to_search = encodeURIComponent($('#sometextfield').val());
$.ajax({
    type: "POST",
    url: ajax_url,
    data: { ajax_vari: "ajax", url_to_search: url_to_search}
    }).success(function(insertID) {
        alert(insertID);
    });

php页面:

if(isset($_POST['ajax_vari']) && $_POST['ajax_vari'] == 'ajax'){
     echo $_POST['url_to_search'];
     echo urlencode($_POST['url_to_search']);
}

URL 示例:

html/ajax

原网址:http://www.llbean.com/llb/shop/77754?feat=502857-GN2&page=men-s-mif-w-baseball-cap-black-bear&attrValue_0=Woodland&productId=1286129

之后encodeURIComponent()http%3A%2F%2Fwww.llbean.com%2Fllb%2Fshop%2F77754%3Ffeat%3D502857-GN2%26page%3Dmen-s-mif-w-baseball-cap-black-bear%26attrValue_0%3DWoodland%26productId%3D1286129

php页面:

之后urldecode()http://www.llbean.com/llb/shop/77754?feat=502857-GN2&page=men-s-mif-w-baseball-cap-black-bear&attrValue_0=Woodland&productId=1286129

没有urldecode()http://www.llbean.com/llb/shop/77754?feat=502857-GN2&page=men-s-mif-w-baseball-cap-black-bear&attrValue_0=Woodland&productId=1286129

谢谢

4

0 回答 0