如何将$ .post表单转换为$ .ajax表单?

时间:2013-11-27 22:32:28

标签: php jquery ajax

我是使用jquery和ajax的noob。我需要将表单从$ .post更改为$ .ajax。

   var disqus_config = function() {

            this.callbacks.onNewComment = [function(comment) {

                    $.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){

                            alert(result);

                    });

            }];
    };

我知道我需要结束这样的事情,但我不知道如何在这个功能中使用帖子数据(评论,帖子,作者)

   $.ajax({
         url: 'sendnotification',
         type: 'POST',
         data: 'query=' + query ,
         dataType: 'JSON',
         async: true,
         success: function(data){
            process(data)
         }

由于

2 个答案:

答案 0 :(得分:1)

只需使用您为$.post所做的相同对象文字,例如(假设那里有一些PHP或其他东西)

$.ajax({
     url: 'sendnotification',
     type: 'POST',
     data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
     dataType: 'json',
     async: true,
     success: function(data){
        process(data)
     }
});

答案 1 :(得分:1)

我认为dataType: 'JSON'应更改为dataType: 'json'
另外,使用与$ .post变体中使用的数据相同的数组。

 $.ajax({
         url: 'sendnotification',
         type: 'POST',
         data: { comment: comment.id, post: $post->id,author:$author->id } ,
         dataType: 'json',
         async: true,
         success: function(data){
            process(data)
         }
});
相关问题