2013-07-21 44 views
-2

javascipt的jQuery的AJAX HTML特殊字符

var a="sadahdka sarr    &nbsp" 

jQuery.ajax(
         { 
          type:"POST", 
          url:"function/postbai.php", 
          data:"a="+a, 
          success:function(html) 
          { 
           alert(html); 
          } 
         }); 

之前

var a="sadahdka sarr    &nbsp" 

后 为什么

$_POST['a']="sadahdka sarr" 

帮助 jQuery的AJAX HTML特殊字符

+0

嗨Sonj。欢迎来到StackOverflow!如果您发现回复可以回答您的问题,请将其选为答案 - 您可以花费尽可能多的时间选择答案。 – TryHarder

+0

请更清楚地问你的问题。不要在问题中写标签。 –

回答

3

b因为&是查询字符串中的字段分隔符。如果你想保留&确保你做encodeURIComponent(a);

+0

谢谢:) :)) – Sonj

1

您的字符串是无效的查询字符串,你要么必须与encodeURIComponent编码,或让jQuery的为你做它通过传递一个对象:

var data = {a :"sadahdka sarr    &nbsp"}; 

jQuery.ajax({ 
    type: "POST", 
    url: "function/postbai.php", 
    data: data 
}).done(function (html) { 
    alert(html); 
}); 
0

在将字符串发送到服务器并使用PHP解码之前,先将其转义。

JS:

data : "a="+escape(a); 

PHP:

urldecode($_POST['a']); 
+0

谢谢谢谢谢谢谢谢)))))))))))))))))) – Sonj

+0

你可能想看看'encodeURIComponent',因为它更适合转义URL的字符 – War10ck