Ajax Call Success,Post实际上没有发送

时间:2013-11-01 00:19:33

标签: javascript php ajax json

我尝试删除JSON.stringify并更改帖子以将更改缓存设置为false和true。我不知道需要发生什么。它总是转到我的php中的else语句并返回默认的JSON。我已经允许我的php中的跨域使用通配符,所以这绝对不是问题。

代码:

$(document).ready(function() {
$('.check').click(function(){

var thisID    = $(this).attr('id');
alert(thisID);
$.ajax({
    type: "POST",
    crossDomain: true,
    url: "retrieveColumn.php",
    data: JSON.stringify({ ID: thisID}),
    cache: true,
    async:true,
    datatype: "json",
    success: function(data)
    {
        console.log(data);
        alert(data);
    }

});
}); 
});

总是进入其他条件的PHP:

if(isset($_POST['ID']))
{
$ID = $_POST['ID'];

{

$stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = $ID ");

 if($stmt->num_rows) //if there is an ID of this name
 {  
$row = $stmt->fetch_assoc();
echo $row;
 print json_encode($row);   

}
}
 }
else
{
 $stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = 2");
  $row = $stmt->fetch_assoc();
 print json_encode($row);   
}

2 个答案:

答案 0 :(得分:1)

除非这是较大文档的一部分,否则您可能会出现不必要的括号,这可能会导致问题。

if(isset($_POST['ID'])){
$ID = $_POST['ID'];

{ /* <!-- HERE! What is this?? */

$stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = $ID ");

 if($stmt->num_rows) //if there is an ID of this name{  
 $row = $stmt->fetch_assoc();
 echo $row;
 print json_encode($row);   

}
}
 }
else
{
 $stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = 2");
  $row = $stmt->fetch_assoc();
 print json_encode($row);   
}

答案 1 :(得分:0)

您不需要将您提交的对象字符串化为数据:

data: JSON.stringify({ ID: thisID}),

使用:

data: { ID: thisID},
相关问题