简体   繁体   中英

Format Json returned by AJax call and assign it to javascript variable

I have script that return this from the server using ajax call

//ajax call
var comment_frm = $('#comment_form');
                comment_frm.submit(function (ev) {
                    $.ajax({
                        type: comment_frm.attr('method'),
                        url: comment_frm.attr('action'),
                        data: comment_frm.serialize(),
                        success: function (data) {
                            if (data == 1){
                                $("#success_message").show();
                                $('#comment_form').trigger("reset");
                            }
                        },
                        error: function (jXHR, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                    //prevent the page from loading
                    ev.preventDefault();
                });

[{"commentID":"5","name":"name 1","comment":"comment 1","comment_time":"1460652379","blog_unique_id":"19","comment_status":"1"},{"commentID":"6","name":"name 2","comment":"comment 2","comment_time":"1460652387","blog_unique_id":"19","comment_status":"1"},{"commentID":"7","name":"name 3","comment":"comment 3","comment_time":"1460652416","blog_unique_id":"19","comment_status":"1"},{"commentID":"8","name":"name 4","comment":"comment 4","comment_time":"1460652425","blog_unique_id":"19","comment_status":"1"},{"commentID":"9","name":"name 5","comment":"comment 5","comment_time":"1460652433","blog_unique_id":"19","comment_status":"1"}]

I want to format it to look like this and assign the json result to a javascript variable

var comment_array = {"Comments": [{"commentID":"5","name":"name 1","comment":"comment 1","comment_time":"1460652379","blog_unique_id":"19","comment_status":"1"},{"commentID":"6","name":"name 2","comment":"comment 2","comment_time":"1460652387","blog_unique_id":"19","comment_status":"1"},{"commentID":"7","name":"name 3","comment":"comment 3","comment_time":"1460652416","blog_unique_id":"19","comment_status":"1"},{"commentID":"8","name":"name 4","comment":"comment 4","comment_time":"1460652425","blog_unique_id":"19","comment_status":"1"},{"commentID":"9","name":"name 5","comment":"coment 5","comment_time":"1460652433","blog_unique_id":"19","comment_status":"1"}]}



 //php code
    $operation = $_POST['operation'];
if($operation == 'add_comment'){
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    $blog_id = $_POST['blog_id'];
    $comment_status = 1;
    if ($me->add_comment($name, $comment, $blog_id)){
        //get the comment
        $comment_array = $me-> fetch_moderated_comment($blog_id);
        echo json_encode($comment_array);
    }else{echo 10;}
}

I will also love to know how to read get that from the call... Can someone please help me

Working Example

You could assign a variable to hold your new 'Comments' array and then push to it:

var a = [{"commentID":"5","name":"name 1","comment":"comment 1","comment_time":"1460652379","blog_unique_id":"19","comment_status":"1"},{"commentID":"6","name":"name 2","comment":"comment 2","comment_time":"1460652387","blog_unique_id":"19","comment_status":"1"},{"commentID":"7","name":"name 3","comment":"comment 3","comment_time":"1460652416","blog_unique_id":"19","comment_status":"1"},{"commentID":"8","name":"name 4","comment":"comment 4","comment_time":"1460652425","blog_unique_id":"19","comment_status":"1"},{"commentID":"9","name":"name 5","comment":"comment 5","comment_time":"1460652433","blog_unique_id":"19","comment_status":"1"}]

var b = {"Comments": []};

for (var prop in a) {
  b.Comments.push(a[prop]);
}
echo json_encode(['Comments' => $comment_array]);

如果我理解正确,请将结果包装在对象文字中:

vat result = {"Comments": comment_frm};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM