W
W
WhatIsHTML2018-12-19 16:06:12
JavaScript
WhatIsHTML, 2018-12-19 16:06:12

Will the request be terminated if there is a redirect to another page?

There are two requests sent from the frontend.
Request #1 goes to our server, is processed and sent to a third-party server. We do not expect a response from this request on the frontend.
Request number 2 goes to our server, is processed, we send a response to the frontend, then a redirect to another page.
Both requests are asynchronous. Will Request #1 be terminated if the redirect occurred faster than the request from our server to the third-party server could be sent?
Frontend:

request1();
request2();

function request1() {
$.ajaxSetup({
   headers: { 'X-CSRF-Token' : '{!! csrf_token() !!}' }
});
$.ajax({
    type: "POST",
    url: "{{ url() }}/url-1",
    data: data,
    dataType: 'json',
    error: function (data) {
      console.log(data)
    },
    success: function (data) {
        // do nothing
        console.log(data)
    }
}); // End AJAX
}

function request2() {
$.ajaxSetup({
   headers: { 'X-CSRF-Token' : '{!! csrf_token() !!}' }
});
$.ajax({
    type: "POST",
    url: "{{ url() }}/url-2",
    data: data,
    dataType: 'json',
    error: function (data) {
      console.log(data)
    },
    success: function (data) {
        // redirect
        window.location.href = newUrl;
    }
}); // End AJAX
}

Backend sending a request to a third-party server after request1() from the frontend
{
      $listID = 2;
      $segment = parse_url($request->headers->get('referer'))["path"];

      if ($segment == '/some-url') {
        $listID = 8;
      }

      $client = new Client();
      $res = $client->request('POST', 'XXX', [
            'form_params' => [
                'api_key' => 'XXX',
                'actid' => 'XXX',
                'api_action' => 'XXX',
                'api_output' => 'json',
                'p[listID]' => $listID,
            ]
      ]);

      echo $res->getBody();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-12-19
@xpert13

Will Request #1 be terminated if the redirect occurred faster than the request from our server to the third-party server could be sent?

Will not. If the request has reached your server and it starts processing it, then it will finish what it started, even if no one is waiting for the answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question