0

我正在使用 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);
    }

我的语法有问题还是其他问题?

4

2 回答 2

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"];
       ......
于 2013-05-16T19:44:47.700 回答
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(){});

于 2013-05-16T19:44:44.703 回答