dongyanggan3025 2015-11-11 16:18
浏览 61
已采纳

Laravel 5.0 ajax消息提交返回错误或超时

Currently, I am attempting to use ajax to send a message (with instant messenger) to the server, save the message and update with the message. Whenever I submit it, however, it returns a 500 Internal Server Error and if I change the token:$('#token') to '_token' : token as I have seen here, it times out completely.

The ajax

function submitIMchat(){
        console.log($('#IMchat-submit-message').val());
        console.log($('#token').val());
        $.ajax({
            url: "{{ action('MessagesController@sendAjaxMessage') }}",
            type: "post",
            dataType:'json',
            data: ({ token:$('#token'), id:{{ Session::get('IMchatuser') }}, message:$('#IMchat-submit-message').val() }),
            success: function(data){
                console.log(data);
                return false;
                }

        })
        .error(function(data) {
        var errors = data.responseJSON;
        console.log('server errors',errors);
        });
    };

The form

<form action="#" onsubmit="submitIMchat()">
    <input type="hidden" id="token" value="{{ csrf_token() }}">
    <input id="IMchat-submit-message" type="text" style="width:100%;">
</form>

The route

Route::post('sendAjaxMessage', 'MessagesController@sendAjaxMessage');

Can anyone tell what might be wrong with my code here? If any other information is needed, please let me know. Note: I did not include sendAjaxMessage as it does not execute the function yet.

Thanks.

  • 写回答

1条回答 默认 最新

  • doumen9709 2015-11-11 21:46
    关注

    I finally solved the issue. It was related to how I was getting my variables in the sendAjaxMessage function: I was treating it like a request when I needed to grab the form values using Input::all() and set them as individual variables. Also, the message sent to the function ended up being saved as $message while the Message object that was created and saved was also named $message. Thus, $message->message = $message would break.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功