Laravel 4.2 and AJAX POST - 500 Internal Server Error

倾然丶 夕夏残阳落幕 提交于 2020-01-03 04:57:08

问题


I am puzzled by the following problem:

I have a code that looks like this

<form action="{{ URL::route('user-send-message') }}" method="post" id="form-user-send-message">

@if( $user_message_block )
    <input id="msg-id" name="msg-id" type="hidden" value="{{ $user_message_block->id }}">
@else
    <input id="msg-id" name="msg-id" type="hidden" value="0">
@endif

    <input id="getter" name="getter" type="hidden" value="{{ $profile->user->username }}">
    <textarea id="user-message" name="user-message"></textarea>
    <button class="btn btn-success btn-xs pull-right" type="submit" name="btn-send-message">Send</button>
    <div class="clearfix"></div>
{{ Form::token() }}
</form>

<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#form-user-send-message').on('submit', function(){          
           // ajax post method
            $.post(
                $(this).prop('action'),{
                    "_token": $( this ).find( 'input[name=_token]' ).val(),
                    "msg-id": $( '#msg-id' ).val(),
                    "getter": $( '#getter' ).val(),
                    "user-message": $( '#user-message' ).val()
                },
                function(data){
                    $(".message-area").append('<div class="message-user">' + data['who'] + ' said:</div>'
                        +'<div class="message-sent">' + data['when'] + '</div>'
                        +'<div class="message-viewed"><em>' + data['seen'] + '</em></div>'
                        +'<div class="clearfix"></div>'
                        +'<div class="message-text seen-no">' + data['text'] + '</div>');
                    $('.message-area').scrollTop($('.message-area')[0].scrollHeight);
                    $('#user-message').val('');
                },
                'json'
            ); 
            return false;
        }); 
    });
</script>

When I remove this portion of code

@if( $user_message_block )
    <input id="msg-id" name="msg-id" type="hidden" value="{{ $user_message_block->id }}">
@else
    <input id="msg-id" name="msg-id" type="hidden" value="0">
@endif

and I replace it with this

<input id="msg-id" name="msg-id" type="hidden" value="{{ $user_message_block->id }}">

everything works just fine.

Is there something about the If-else condition that would affect AJAX to produce 500 error. The HTML renders correctly,,

Can someone maybe un-puzzle this for me.

Thanks!

This is my network error preview

error: {type:Symfony\Component\Debug\Exception\FatalErrorException,…}
file: "F:\bol\app\controllers\ProfileController.php"
line: 384
message: "Call to a member function count() on a non-object"
type: "Symfony\Component\Debug\Exception\FatalErrorException"

回答1:


Go to debug tools -> network -> click the red 500 internal server error, and edit your OP with the "preview" (if on chrome).



来源:https://stackoverflow.com/questions/26229608/laravel-4-2-and-ajax-post-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!