对于Laravel和Ajax来说相当新,所以如果我犯了一个愚蠢的错误,你会原谅我。我试图从控制器返回响应,当我在路由文件中执行所有逻辑时,它可以正常工作,但是当我将代码复制到我的控制器时,它不起作用。
继承我的路线
Route::post('student', 'StudentPDetailsController@store');
和js文件,名为custom.js
(function($){
$(document).ready(function(){
$('#saveDetails').click(function(e){
e.preventDefault();
var dataString = {'studentphonenumber':$('#sphonenumber').val(),
'studentidnumber':$('#sidpp').val(),
'studentgender':$('#sgender').val(),
'studentbirthdate':$('#datepicker').val(),
'_token':$('#token').val()
}
$.ajax({
type: 'POST',
url: 'student',
data: dataString,
success: function(data){
console.log(data);
alert('Your personal details have been updated')
}
});
});
});
})(jQuery);
继承刀片模板:
<div class="panel panel-default panelshadow">
<div class="panel-heading"><h3 class="headinpanel">Personal Details</h3></div>
<div class="panel-body">
<form class="form-horizontal" id="studentPersonalDetails" role="form" method="POST" action="#">
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Phone Number</label>
<div class="col-md-6">
<input type="text" class="form-control" id="sphonenumber" name="phone" value="{{ old('phone') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">National ID/Passport</label>
<div class="col-md-6">
<input type="text" class="form-control" id="sidpp" name="idnumber" value="{{ old('idnumber') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Gender</label>
<div class="col-md-6">
<select name="gender" id="sgender" class="form-control">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Date of Birth</label>
<div class="col-md-6">
<input type="text" id="datepicker" class="form-control"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary saveBtn" id="saveDetails">
<h4>Save Details</h4>
</button>
</div>
</div>
</form>
</div>
</div>
最后是StudentPDetailsController
public function store()
{
if(Request::ajax()){
return Response::json(Request::all());
}
}
答案 0 :(得分:0)
您需要将Response对象传递给控制器:
public function store(Request $request)
...
$request->all()