【问题标题】:how to pass special characters in ajax call如何在ajax调用中传递特殊字符
【发布时间】:2020-09-05 18:35:42
【问题描述】:

如何在 ajax 调用中传递特殊字符变量 i 并在 spring 控制器中获取值。如果文件name="##@jsuduu"

function deleeteimg(){
    var filename=$("#filenm").text();
    alert(filename);
    //var filename=$(this).attr("data-id");
    var url="/myuploads/deleteFile.html?filename="filename;
    $.ajax({
        type: "GET",
        url: url,
        contentType: 'html',
        success: function(response) {
             window.location.href="/myuploads/showUploads.html";
        }
    });
}

【问题讨论】:

  • 它们是 URL 编码的。
  • encodeURIComponent(filename)
  • 您在这里缺少+ 标志:?filename=" + filename;
  • 嗨 pawel,这个方法已经尝试过了,但它在 ajax url 中不起作用错误告诉我替代方法

标签: javascript jsp


【解决方案1】:

使用以下方法有效

encodeURI()
encodeURIComponent()

type: "POST",
url: "ajax/save",
data:"Name="+encodeURIComponent("##@jsuduu")

【讨论】:

  • 我们用js解码,现在用php怎么解码?
【解决方案2】:

encodeURIComponent() 编码特殊字符。此外,它还对以下字符进行编码: , / ? : @ & = + $ #

您可以尝试在encodeURIComponent() 方法中添加特殊字符,它可以工作。以下是一个示例。

 $.ajax({
        type: "GET",
        url: 'removeFile?removeFilePath=' + encodeURIComponent("C:///YO/Ed/PO/")
        data: {},
        dataType: 'json',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 2015-03-13
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多