我有使用AJAX提交的表格,但是当我尝试提交时却失败了。 错误的console.log是:
POST http://127.0.0.1:8000/quiz/save-question-result/1 500 (Internal Server Error)
send @ jquery.js:8630
ajax @ jquery.js:8166
(anonymous) @ 1:374
dispatch @ jquery.js:4435
r.handle @ jquery.js:4121
我的表单和Ajax sumbit
@foreach($questions as $question)
<div class="jumbotron" id="jumbotron{{$question->id}}"
@if($question->id != $current_question_id)
style="display: none;"
@endif
>
<p>Question #{{$question->id}}</p>
<p>{{$question->question}}</p>
{!! Form::open(['action'=>['QuizDataController@postSaveQuestionResult', $categoryquiz->id], 'method'=>'post', 'id'=>'frm'.$question->id]) !!}
<div id="answer-radio{{$question->id}}">
<div class="btn-group" data-toggle="buttons">
<li>
<label>
<input type="radio" name="option" value="1" /> {{$question->option1}}
</label>
</li>
<li>
<label>
<input type="radio" name="option" value="2" /> {{$question->option2}}
</label>
</li>
<li>
<label>
<input type="radio" name="option" value="3" /> {{$question->option3}}
</label>
</li>
<li>
<label>
<input type="radio" name="option" value="4" /> {{$question->option4}}
</label>
</li>
</div>
</div>
{!! Form::input('hidden','question_id', $question->id) !!}
{!! Form::input('hidden','time_taken'.$question->id,null,['id'=>'time_taken'.$question->id]) !!}
{!! Form::token() !!}
@if($question->id == $last_question_id)
{!! Form::submit('Last', ['class'=>'btn btn-info']) !!}
@else
{!! Form::submit('Next', ['class'=>'btn btn-info']) !!}
@endif
{!! Form::close() !!}
</div>
@if($questions->count()>1)
@section('script_form')
$.ajaxSetup({
headers:{
'X-CSRF-TOKEN':"{{ csrf_token() }}"
}
});
$(function() {
//console.log({{$question->id}});
//console.log({{$last_question_id}});
{{-- $('#frm{!!$question->id!!}').off('submit'); --}}
$('#frm{!!$question->id!!}').on('submit', function(e){
e.preventDefault();
$.ajax({
url: $('#frm{!!$question->id!!}').attr('action'),
data: $('#frm{!!$question->id!!}').serialize(),
cache: false,
contentType: false,
processData: false,
enctype: "multipart/form-data",
type: 'POST',
success: function(data){
data = JSON.parse(data);
if (data.success)
alert('success!');
else
alert('got failure response from PHP');
},
error:function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
console.log(err.Message);
}
});
{{-- $.post($formAction, $(this).serialize(), function(data){
//if(data.next_question_id != null)
$('#jumbotron{{$question->id}}').css('display','none');
//console.log(data.next_question_id);
$('#jumbotron' + data.next_question_id+'').css('display','block');
}); --}}
});
});