jQuery $.ajax call -- strange return function issue

时间:2015-09-14 16:06:34

标签: javascript php jquery json ajax

I have a $.ajax call which looks as follows:

<script type="text/javascript">
    $(document).ready(function () {
        $('#add_com_frm').submit(function (e) {

            e.preventDefault();
            var $btn_c = $('#add_com_but').button('loading');
            tinyMCE.triggerSave();

            var data = {
                "id": document.getElementById("id").value,
                "body": tinymce.get('body').getContent()
            };

            $.ajax({
               type: "POST",
                url: 'https://something/test.php',
                data: data,
                dataType: 'json',
                success: function(msg,string,jqXHR) {
                    alert(msg.test);
                }
            });

        });
    });
</script>

Now there's something strange, that I just can't explain logically.

PHP side:

 <?php
    $id = $filter->purify($_POST['id']);
    $body = $filter->purify($_POST['body']);

    $today = date('d.m.Y');
    $who = 'Someone';

    $result = $db->insert_com($id, $body, $who, $today);

    $list = array('test' => 'something');
    $c = json_encode($list);
    echo $c;
 ?>

Now, if I comment the line //$result = $db->insert [...] I got the alert from jQuery return msg.test, which is 'something' and that works, but for some strange reason when I un-comment that database related line, even if not directly related to that json being returned, it just doesn't work and no alert visible. I have no logical explanation for that, this line is somehow messing the json encode being returned when echo'ed, but why? and how? No idea!

1 个答案:

答案 0 :(得分:0)

好的,所以看起来原因是我在我的数据库处理程序中有一个include_once到Comment类。基本原理是基于我希望用新数据实现Comment对象的插入,并将其作为html返回。您是否知道如何修改我的调用函数以接受该HTML?