我正在使用ajax发送此数组
代码jquery:
$("#btnReact").on("click", function (e) {
var post = {};
post["comment"] = $("#bug_message").val();
post["id"] = $(this).data("bid");
var request = $.ajax({
url: "ajax/save_comment.php",
type: "POST",
data: {
post: post
},
dataType: "json"
});
request.done(function (msg) {
if(msg.status == "success") {
}
});
request.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
e.preventDefault();
});
但是我无法在php中访问我的数据,并且当我尝试将此数据发送给我的班级时,我一直收到错误。
代码php:
if(isset($_POST["post"]))
{
try
{
$comment = $_POST['post']["comment"];
$id = $_POST['post']["id"];
$comment = new Comment();
$comment->Comment = $comment;
$comment->SaveComment($id);
$feedback['status'] = "success";
}
catch(Exception $e)
{
$feedback['message'] = $e->getMessage();
$feedback['status'] = "error";
}
header('Content-Type: application/json');
echo json_encode($feedback);
}
我的语法有问题还是别的?
答案 0 :(得分:1)
为什么不在数据选项
中的对象内发布对象而不是对象 var request = $.ajax({
url: "ajax/save_comment.php",
type: "POST",
data: post,
dataType: "json"
});
并将其视为
if(isset($_POST["comment"]) && isset($_POST["id"]))
{
try
{
$comment=$_POST['comment'];
$id = $_POST["id"];
......
答案 1 :(得分:0)
试试这个:
jQuery(document).ready(function(){
$("#btnReact").on("click", function(e){
var post = {};
post["comment"] = $("#bug_message").val();
post["id"] = $(this).data("bid");
var request = $.ajax({
url: "ajax/save_comment.php",
type: "POST",
data: {post : post},
dataType: "json"
});
request.done(function(msg) {
if(msg.status == "success"){
}
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
e.preventDefault();
});
});
唯一的区别是我将您的代码放在jQuery(document).ready(function(){});