我想将数据从AJAX传递到我的Laravel Controller。但数据没有通过。这是我的控制器和刀片代码。 数据没有从刀片传递到Ajax。我的控制器没有收到数据。
控制器文件:
public function store(Request $request)
{
$new_name='';
$new_email='';
$new_clg='';
$new_university='';
$new_stream='';
$user = DB::table('users')->select('name', 'email', 'College', 'stream','university')->get();
/* print_r($request->jsonString);*/
foreach($user as $d){
$new_name= $d->name;
$new_email=$d->email;
$new_clg=$d->College;
$new_university=$d->university;
$new_stream=$d->stream;
}
$name1 = explode(",", $request->jsonString);/*
$name2 = explode('["', $name1);
$name3 = explode('"', $name1);*/
$temp = explode('"',$request->jsonString);
// $temp1 = explode('"',$temp);
print_r($temp);
$Q1=$temp[1];
$Q2=$temp[3];
$Q3=$temp[5];
$Q4=$temp[7];
$Q5=$temp[9];
$Q6=$temp[11];
$Q7=$temp[13];
$Q8=$temp[15];
$Q9=$temp[17];
$Q10=$temp[19];
$Q11=$temp[21];
$Q12=$temp[23];
$Q13=$temp[25];
$Q14=$temp[27];
$Q15=$temp[29];
$Q16=$temp[31]; // $this->validate(request(),[
// $user =
// //'answers.*'=> 'required|'
// ]); ["0.3s"]
// $result = 0;
// $test = Test::create([
// 'user_id' => Auth::id(),
// 'result' => $result,
// ]);
$report = master::create([
'user_id' => Auth::id(),
'user_name' => $new_name,
'user_email' => $new_email,
'Stream' => $new_stream,
'University' => $new_university,
'College_Institution' => $new_clg,
'Q1' => $Q1,
'Q2' => $Q2,
'Q3' => $Q3,
'Q4' => $Q4,
'Q5' => $Q5,
'Q6' => $Q6,
'Q7' => $Q7,
'Q8' => $Q8,
'Q9' => $Q9,
'Q10' => $Q10,
'Q11' => $Q11,
'Q12' => $Q12,
'Q13' => $Q13,
'Q14' => $Q14,
'Q15' => $Q15,
'Q16' => $Q16
]);
$report->save();
foreach ($request->input('questions', []) as $key => $question) {
$status = 0;
if ($request->input('options.'.$question) != null
&& QuestionsOption::find($request->input('options.'.$question))
) {
$status = 1;
$result++;
}
else {
return redirect()->back();
}
// TestAnswer::create([
// 'user_id' => Auth::id(),
// 'test_id' => $test->id,
// 'question_id' => $question,
// 'option_id' => $request->input('answers.'.$question),
// 'correct' => $status,
// ]);
}
// DB::insert('insert into master values(?)',[$report]);
//$report->insert()
// $test->update(['result' => $result]);
// return redirect()->route('results.show', [$test->id]);
}
刀片文件:
function endQuiz(){
// $('#explanation').empty();
// console.log(score);
$('#question').empty();
$('#choice-block').empty();
$('#submitbutton').remove();
$(document.createElement('h2')).css({'text-align':'center', 'font-size':'4em'}).text('Congratulation!! You have successfully completed your test.').insertAfter('#question');
var jsonString =JSON.stringify(score);
/*alert(jsonString);*/
$.ajax({
type: "post",
url: "/scores",
dataType: "JSON",
data:{'jsonString':jsonString},
success: function(data){
alert("Test Completed")
// window.location.href = "{{('/home')}}";
},
error: function(data){
alert("Test Completed")
// window.location.href = "{{('/home')}}";
}
});
}
请帮我解决这个问题。
答案 0 :(得分:0)
在Blade文件中,你需要有类似的东西
function endQuiz() {
// Do your stuff here
var jsonString = JSON.stringify(score);
// Make an AJAX request
$.ajax({
type: 'post',
url: '{{ URL::action (‘SomeController@store’) }}',
data: jsonString,
success: function(result) {
if(result.success) {
console.log("Success");
}
},
error: function() {
console.log(error);
}
});
}
在Controller中,您应该返回一个json响应,它可以是数组的组合。像这样的例子
return response(array('success' => true));