Ajax调用仅在Safari中手动触发时才有效,但在chrome

时间:2017-03-07 16:41:11

标签: php ajax laravel

以下是用于向AJAX服务发出请求的代码,它目前在chrome中工作,没有任何问题,但没有任何状态或错误代码。

AJAX request not working correctly

AJAX Resource panel

在控制器中为请求设置响应

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;
&#13;
&#13;

0 个答案:

没有答案