简体   繁体   中英

jQuery $.ajax call — strange return function issue

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!

Ok guys, so it appeared that the reason for that was that I have include_once to Comment class in my database handler. Rationale for that is based on the insertion I wanted to fulfill Comment object with new data, and return it as html. Do you maybe know how to modify my caller function to accept that html?

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