Tuesday, March 7, 2017

Ajax call only works when triggered manually in Safari but works fine in chrome

Below is the code which is being used to make a request to an AJAX service, it currently works in chrome without any issues but fails with out any status's or error codes.

AJAX request not working correctly

AJAX Resource panel

Response is set in the controller for the request

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>


via D.Mercer

Advertisement