【问题标题】:Jquery ajax call with '+' sign带有“+”号的 Jquery ajax 调用
【发布时间】:2011-03-02 15:50:30
【问题描述】:
$.ajax({  
        type: "POST", url: baseURL+"sys/formTipi_azioni",data:"az_tipo="+azione,
        beforeSend: function(){$("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');},
        success: function(html){$("#form").html(html);}  
     });

有一种情况是 azione 是

TB+ 

加号根本不会发布,而是发送一个空格。我已经试过了:

azione = escape(String(azione));

没有运气。有人知道如何解决这个问题吗?

【问题讨论】:

    标签: php javascript jquery ajax post


    【解决方案1】:

    您正在寻找encodeURIComponent

    【讨论】:

      【解决方案2】:
      escape(String(azione)).replace(new RegExp( "\\+", "g" ),"%2B");
      

      这个在正则表达式的帮助下发送加号

      【讨论】:

        【解决方案3】:
        azione = escape(String(azione));
        

        应该是

        azione = encodeURIComponent(String(azione));
        

        或者干脆

        azione = encodeURIComponent(azione);
        

        【讨论】:

        • 类型转换在 JS 中不像那样工作... ((String)azione)
        【解决方案4】:

        试试这个:

        $.ajax({  
            type: "POST", 
            url: baseURL + "sys/formTipi_azioni",
            data: { az_tipo: azione },
            beforeSend: function(){
                $("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');
            },
            success: function(html){
                $("#form").html(html);
            }  
        });
        

        让 jQuery 为你做 url 编码。

        【讨论】:

        • 很好,我从来不知道这是可能的。谢谢。
        • 这是可能的,好处是你不必担心url编码。
        • +1,在这种情况下最好的解决方案。 “永远不要使用escape()”仍然是需要牢记的一点。
        【解决方案5】:

        除了尝试自己编写发布数据之外,您还可以通过传递一个对象让 jQuery 完成工作:

        $.ajax({  
            type: "POST", url: baseURL+"sys/formTipi_azioni",
            data: {az_tipo: azione},
            beforeSend: function(){$("#form").html('<p><img src="'+baseURL+'lib/img/ajax-loader.gif" width="16" height="16" alt="loading" /><p>');},
            success: function(html){$("#form").html(html);}  
         });
        

        【讨论】:

          【解决方案6】:

          永远不要使用escape()。使用encodeURIComponent()

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-10-15
            • 2011-10-13
            • 2012-01-15
            • 1970-01-01
            • 2012-12-06
            • 2016-11-25
            • 2011-09-08
            • 2013-02-01
            相关资源
            最近更新 更多