【问题标题】:500 internal server error while posting data to database with laravel 5 and ajax使用 laravel 5 和 ajax 将数据发布到数据库时出现 500 内部服务器错误
【发布时间】:2015-07-24 15:18:23
【问题描述】:

大家好?我正在尝试使用 laravel 5 和 ajax 将数据发布到数据库。我还通过添加来应用 csrf 保护

<meta name="_token" content="{!! csrf_token() !!}"/>

到我的布局页眉并将以下代码添加到我的页脚:

<script type="text/javascript">
$.ajaxSetup({
   headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
</script>

这是我的表格:

<form action="{{action('QuizController@postQuiz')}}" method="POST">
<div id="name-group" class="form-group">
<label for="name">Please type your question here</label>
<input type="text" class="form-control" name="question">
</div>
<button type="submit" class="btn btn-success">Submit <span class="fa fa-arrow-right"></span></button>
</form>

这是我的 JS 代码:

var formData = {
'question'    : $('input[name=question]').val(),
};
// process the form
$.ajax({
type : 'POST',
url : 'quiz',
data : formData, 
dataType : 'json', 
encode : true
})
// using the done promise callback
.done(function(data) {

// log data to the console to see
console.log(data); 


// ALL GOOD! just show the success message!
$('form').append('<div class="alert alert-success">' + data.message + '</div>');

// stop the form from submitting the normal way and refreshing the page
event.preventDefault();

这是我的路线:

Route::post('create/quiz', array(
'as' => 'post-quiz',
'uses' => 'QuizController@postQuiz'
));

当我的控制器如下:

public function postQuiz()
{
if(Request::ajax()) {
$question = Request::get('question');
$data['success'] = true;
$data['message'] = $question;
echo json_encode($data);
   }

ajax 调用有效并返回,

Object {success: true, message: "test question"}

但是当我尝试使用以下方式将数据发布到数据库时:

public function postQuiz()
{
if(Request::ajax()) {
$question = Request::get('question');
DB::table('questions')->insert([
'question' => $question,
]);
     }

我从控制台得到以下信息

POST http://localhost/leoschool-laravel5/public/create/quiz 500 (Internal Server Error)

Object {readyState: 4, responseText: "{"success":true,"message":"test question"}<!DOCTYPE htm…l>↵</div>↵↵            </div>↵    </body>↵</html>", status: 500, statusText: "Internal Server Error"}

可能是什么问题?谢谢..

【问题讨论】:

  • 查看服务器错误日志以获取有关 500 的详细信息。
  • 难怪会抛出500错误。 'question' =&gt; $question, , 在数组有 1 个项目时使用。从数组中删除 ,。
  • @Rajlaksh:PHP 中允许数组中最后一个元素后的逗号
  • 你查看 storage/log/laravel 日志了吗?会有致命错误什么的

标签: php jquery ajax laravel


【解决方案1】:

Chrome 开发者工具是一个很好的起点。加载您的页面并打开工具并触发执行 AJAX 请求的事件。

在工具的网络选项卡下,它将向您显示每个请求,并允许您预览响应,就像您没有使用 AJAX 一样。这将向您显示 laravel 堆栈跟踪。我认为问题在于您正在使用外观并且它们的命名空间不正确。

将你的控制器功能改成这个,看看它是否有效:

public function postQuiz()
{
    if(\Request::ajax()) {
        $question = \Request::get('question');
        \DB::table('questions')->insert([
            'question' => $question,
        ]);
}

通过以上关于如何使用开发工具的说明以及更正的代码,您应该能够解决您的问题。不过,编写此代码的更好方法如下所示:

// assuming you have these models setup
// this uses dependency injection
public function postQuiz(Request $request, Question $question)
{
    if($request->ajax()) {
        $newQuestion = $request->get('question');
        //add fields here to create new question with
        $question->create([ /*stuff*/ ]);
}

【讨论】:

  • 非常感谢凯尔..愚蠢的错误,但你是对的,我没有命名 laravel 外观数据库。非常感谢:-)
猜你喜欢
  • 2019-03-20
  • 2019-05-24
  • 2018-01-24
  • 2014-07-02
  • 2019-05-18
  • 2018-03-12
  • 2018-09-30
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多