duan0530
2016-10-10 09:31
浏览 28

试图通过JQuery Ajax将特殊字符发送到PHP脚本

I'm sending special characters to a PHP file via JQuery Ajax.

send_to_process.js

var special_charac = '!@#$%^&*()_+-=';
var dataString = 'data=' + special_charac;

$.ajax({
                type: "POST",
                url: "./process.php",
                data: dataString,
                cache: false,

                success: function (result) {

                }
            });

process.php

<?php

$data= $_POST['data'];
echo $data;

?>

In the PHP file I'm getting all values except + and &

Why is it so ?

Does JQuery Ajax has got some limitations as to what data can you send to PHP script ?

图片转代码服务由CSDN问答提供 功能建议

我通过JQuery Ajax将特殊字符发送到PHP文件。

send_to_process.js

  var special_charac ='!@#$%^&amp; *()_ +  -  ='; 
var dataString =  'data ='+ special_charac; 
 
 $ .ajax({
 type:“POST”,
 url:“。/ process.php”,
 data:dataString,
 cache:false,
  
成功:功能(结果){
 
} 
}); 
   
 
 

process.php

 &lt;?php 
 
 $ data = $ _POST ['data']; 
echo $ data; 
 
?&gt; 
   
 
 

在PHP文件中我得到的所有值除了 + &amp;

为什么是 是这样吗?

JQuery Ajax对于您可以向PHP脚本发送哪些数据有一些限制吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi1207 2016-10-10 09:34
    已采纳

    These are not AJAX limitations. These are URL limitations. eg & is used to split parameters. Just send data as json object

    not:

    data: dataString,
    

    but

    data: {data: special_charac}
    

    or use encodeURI function to escape data

    var dataString = 'data=' + encodeURI(special_charac);
    
    打赏 评论

相关推荐 更多相似问题