$ .post添加双反斜杠

时间:2011-11-08 16:28:36

标签: php jquery

我使用jQuery将数据从客户端发送到服务器并将数据存储在我的数据库中。现在我使用jQuery中的$ .post-function。我使用它像下面的

var queryUpd = "UPDATE settings SET round_duration='10'"
$.post("../../server/test.php", {func: "manipulate", query: queryUpd}, function(json,    textStatus) 
 {
alert(json); //Outputs UPDATE settings SET round_duration =\\\'10\\\'           
 });

php函数“test.php”很简单:

<?php
  echo json_encode($_POST["query"]);  //send values to the client
?>

正如你所看到的,php-function获得了一个无效的查询,因为它在\'前面添加了两个反斜杠。那么为什么会发生这种情况呢?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可能需要指定您获得的是JSON。否则,jQuery猜测,有时它是错误的:

$.post("../../server/test.php", {func: "manipulate", query: queryUpd}, function(json,    textStatus) 
     {
    alert(json); //Outputs UPDATE settings SET round_duration =\\\'10\\\'           
     },"json");

但是,您原来帖子的评论是正确的。您不应该通过ajax发送查询。您应该在检查表单数据后发送表单数据并根据该数据创建查询,以确保它有效。

答案 1 :(得分:0)

如果它是php,那么在你的应用程序的bootstrap中试试

<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_gpc(&$value)
    {
        $value = stripslashes($value);
    }
    array_walk_recursive($_GET, 'stripslashes_gpc');
    array_walk_recursive($_POST, 'stripslashes_gpc');
    array_walk_recursive($_COOKIE, 'stripslashes_gpc');
    array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
?>
相关问题