我正在使用Laravel作为My FrameWork,当尝试使用Laravel使用AJAX请求时感到震惊。因为我所有的代码都在下面:
路线(Laravel)
/*-- Welcome Page Handler --*/
Route::get('/{pages}', 'UsersController@project' );
Route::get('/', 'UsersController@project' );
控制器(Laravel)
public function project( Request $request, $pages = 'index' ) {
... authetication section ...
if( $getAccess ) {
define( "REALNAME" , $getAccess->realname );
define( "PERMISSION" , $getAccess->permission );
define( "CABANG" , $getAccess->cabang );
define( "COMPANY" , $getAccess->company );
return view( 'index' , [
'pages' => $pages ,
'params' => 'main' ,
'title' => ucwords( $pages ) ,
'headertitle' => 'Easy CRM' ,
'request' => $request
] );
} else {
return view( 'login' , [
'title' => 'Login' ,
'headertitle' => 'Easy CRM'
] );
}
}
JavaScript部分(内部部分)
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'GET',
url: 'ajax_save',
data: {text:$('#txt').val()},
dataType: "text",
success: function(msg) {
alert(msg);
$("#hasil").html(msg);
}
});
});
});
</script>
HTML部分
<div class="col-md-12">
<input type="text" id="txt" name="text" />
</div>
<div class="col-md-12">
<span id="text1">Result Maybe Here</span>
</div>
<div class="col-md-12">
<button>Get External Content</button>
</div>
我所能做的就是,结果冻结了,什么也没发生。情况很简单,请在下面显示用户类型(id text1)。
更新1:
<script src="http://{{ SERVERNAME }}/libraries/jquery/jquery-3.3.1.min.js"></script>
<script src="http://{{ SERVERNAME }}/libraries/popper/popper.min.js"></script>
<script src="http://{{ SERVERNAME }}/bootstrap/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'ajax_save',
data: {text:$('#txt').val()},
dataType: "text",
success: function(msg) {
alert(msg);
$("#hasil").html(msg);
}
});
});
});
</script>
错误代码419(未知状态)
更新2:
<script src="http://{{ SERVERNAME }}/libraries/jquery/jquery-3.3.1.min.js"></script>
<script src="http://{{ SERVERNAME }}/libraries/popper/popper.min.js"></script>
<script src="http://{{ SERVERNAME }}/bootstrap/js/bootstrap.min.js"></script>
<script src="http://{{ SERVERNAME }}/rajawali/js/ajax-request.min.js"></script>
ajax-request.min.js
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).ready(function(){
$("#password").click(function(){
$.ajax({
type: 'POST',
url: 'ajax_save',
data: {text:$('#username').val()},
dataType: "text",
success: function(msg) {
alert(msg);
$("#hasil").html(msg);
}
});
});
});
由于Laravel使用CSRF保护,以及如何通过此方法或某种方法,所有更新仍然产生相同的结果错误代码419(未知状态)