NetworkError:Ajax $ post请求中的500内部服务器错误

时间:2013-08-13 15:43:10

标签: php javascript ajax codeigniter

我正在尝试创建一个聊天框并在Codeigniter中执行此AJAX帖子但由于某种原因我得到服务器500内部服务器错误并且当我使用firebug时根本没有响应?有人可以帮忙吗?

这是我的Javascript代码

$(document).ready(function(){

    $("a#submit").click(function(){

        var chat_message_content = $("input#chat").val();

        if(chat_message_content == ""){

            return false;
        }

        $.post(base_url + "index.php/abovetheblues/add_chat_messages", {
            chat_message_content : chat_message_content, 
            user_id : user_id
        }, 
        function(data){

            alert(data);
        },"json");


        return false;
    });
    return false;

});

这是我的控制器

function add_chat_messages() {
        // Grab the $chat_message_content, $user_id
        $user_id = $this->input->post($this->session->userdata("user_id"));
        $chat_message_content = $this->input->post('chat_message_content');

        $this->abt_db->add_chat_message($user_id, $chat_message_content);
    }

这是我的模特

function add_chat_message($user_id, $chat_message_content) {

    $query_str = "INSERT INTO chat_message(user_id, chat_message_content) VALUES (?,?,?)";
    $this->db->query($query_str, array($user_id, $chat_message_content));
}

我的观看页面

<script type="text/javascript">

    var user_id = "<?php echo $this->session->userdata("user_id"); ?>";



</script>

<!--loads the header-->
<?php $this->load->view('abt-header'); ?>
<!--this is the login page-->

<div data-role="page" id="Abt-chat" data-add-back-btn="true">
    <div data-role="header" data-position="fixed">
        <h1>Peer Chat</h1>

    </div>
    <div data-role="content">

        <div data-role="fieldcontain">
            <div id="chat_viewport"></div>

            <p>
                <label>Input Chat: </label>
                <input name="chat" id="chat" type="text" value=""/>

            </p>

            <p>
                <?php echo anchor('#', 'Send Chat', array('title' => 'Send Chat', 'id' => 'submit')); ?>
            </p>  

        </div> 
        <?php echo form_close(); ?>

    </div>

</div>

1 个答案:

答案 0 :(得分:0)

您正在将用户ID传递为user_id。但是在功能中检索它时你会犯错误。改变如下。

$user_id = $this->input->post('user_id');

还有 - &gt;需要在query()

之前出现
$this->db->query($query_str, array($user_id, $chat_message_content));