以下是用于向AJAX服务发出请求的代码,它目前在chrome中工作,没有任何问题,但没有任何状态或错误代码。
AJAX request not working correctly
在控制器中为请求设置响应
return response()->json(['hello' => $value])->setStatusCode(Response::HTTP_OK, Response::$statusTexts[Response::HTTP_OK]);
< script type = "text/javascript" >
var startTime = new Date().getTime();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(window).on('beforeunload ', function() {
doLeavingAjax();
});
var currentPage = window.location.pathname;
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
function doLeavingAjax() {
var endTime = new Date().getTime();
var timeSpent = endTime - startTime;
var i = 0;
$.ajax({
url: '/PSTracking/leave',
method: 'POST',
dataType: 'json',
async: 'false',
data: JSON.stringify({
"timeSpent": timeSpent
}),
contentType: "application/json; charset=utf-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("_token", CSRF_TOKEN);
}
});
return false;
}
<
/script>
&#13;