简体   繁体   English

jQuery $ .ajax调用—奇怪的返回函数问题

[英]jQuery $.ajax call — strange return function issue

I have a $.ajax call which looks as follows: 我有一个$ .ajax呼叫,看起来如下:

<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方面:

 <?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. 现在,如果我注释行// $ result = $ db-> insert [...],我从jQuery收到警报,返回msg.test,这是“某事”,并且可以工作,但是由于某些奇怪的原因,当我取消-注释与数据库相关的行,即使与返回的json不直接相关,也行不通并且看不到警报。 I have no logical explanation for that, this line is somehow messing the json encode being returned when echo'ed, but why? 我对此没有逻辑解释,这行在某种程度上弄乱了在回显时返回的json编码,但是为什么呢? 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. 好的,看来这是因为我的数据库处理程序中有include_once to Comment类。 Rationale for that is based on the insertion I wanted to fulfill Comment object with new data, and return it as html. 这样做的理由是基于我想用新数据实现Comment对象并将其返回为html的插入。 Do you maybe know how to modify my caller function to accept that html? 您也许知道如何修改我的调用者函数以接受该HTML?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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