我使用jquery ajax在laravel中存储表单中的数据。它完美地存储了数据。但success
功能的问题。它说POST http://localhost:8000/insertteammessage 500 (Internal Server Error)
。
这是我的路线
Route::post('insertteammessage','PublicController@insertTeamMessage')->name('insertteammessage');
这是我的控制器
public function insertTeamMessage(Request $request){
$this->layout = null;
if(Request()->ajax()){
$id = Input::get( 'projectId' );
$message = Input::get( 'teamMessage' );
$teamChat = new TeamChat;
$teamChat->team_message = $message;
$teamChat->projectproposal_id = $id;
$teamChat->user_id = 1;
$teamChat->unread = 1;
$teamChat->save();
$response = array(
'status' => 'success',
'msg' => 'successfull',
);
return Response::json($response);
}else{
return 'no';
}
}
这是我的观点
{!! Form::open(array('route' => 'insertteammessage','id'=>'team-chat')) !!}
<input type="hidden" class="form-control" id="projectId" name="projectId" value="{{$projectid}}"
>
<input type="text" class="form-control" id="teamMessage" name="teamMessage" autofocus>
<button class="btn btn-success" style="float: right;">Send</button>
{!! Form::close() !!}
这是我的剧本
$('#team-chat').on('submit', function(e){
e.preventDefault();
var teamMessage = $('#teamMessage').val();
var projectId = $('#projectId').val();
$.ajax({
type:'POST',
url: '{{URL::to("/insertteammessage")}}',
data: $(this).serialize(),
success: function(msg) {
console.log(msg);
}
});
});
答案 0 :(得分:0)
我已经解决了这个问题。问题出在Response facade上。我忘了把它添加到我的控制器中。看到@tudor评论后,我检查了我的错误日志,发现了这个错误。然后我在我的控制器中使用了use Response;
。现在问题解决了。