1

I have an AJAX POST that is sending back a successful response from my PHP file newPNRsubmit.php :

if(isset($_POST['ticketentry'])){
            $_SESSION['ticketEntry'] = 1;
            header("location: pnr-details?id=".$pnrid);
        }

However I would like to use the AJAX response that I am receiving into window.location

This is the response:

XHR finished loading: GET "https://example.com/pnr-details?id=240".

This is my AJAX structure:

$.ajax({
 type: "POST",
 url: "newPNRsubmit.php",
 data: {
 bookingdate: bookingdate,
 airline_id: airline_id
},
 cache: false,
 success: function(data, url) {
 $('#NewPNRModal').modal( 'hide' );
},
 error: function(xhr, status, error) {
 console.error(xhr);
 }
 });
 return false;

How can I correctly parse the URL that I am receiving into window.location ?

UPDATE: This is the response from console tab. The second GET XHR is showing the link I need for window.location. Should I update the header location to send JSON data in my PHP file in order for this to work?

enter image description here

Leb_Broth
  • 1,081
  • 1
  • 15
  • 32

1 Answers1

3

Either return a URL in the body instead of a header, or get the header like so:

success: function(data, URL, jqXHR) {
  window.location.href = jqXHR.getResponseHeader("location");
}
HTMHell
  • 5,761
  • 5
  • 37
  • 79
  • I was thinking about this solution too, but I was wondering if the browser won't simply follow the `location` header and we won't see it. Well, it seems that you can only read it if you add `Access-Control-Expose-Headers: Location` in your CORS, according to this post: https://stackoverflow.com/a/15444439/653182 – Patrick Janser Jun 25 '21 at 14:16
  • @htmhell I did it but it is redirecting now to a NULL url – Leb_Broth Jun 25 '21 at 14:20
  • Are you sending the request from the same domain? If not, it's probably CORS problem. In any case, try to `console.log(jqXHR.getAllResponseHeaders());` and see what's the result. – HTMHell Jun 25 '21 at 14:23
  • And what's the output you see on the console? – HTMHell Jun 25 '21 at 14:24
  • @HTMHell this is the output: `cache-control: no-store, no-cache, must-revalidate content-encoding: br content-type: text/html; charset=UTF-8 date: Fri, 25 Jun 2021 14:25:10 GMT expires: Thu, 19 Nov 1981 08:52:00 GMT pragma: no-cache server: LiteSpeed vary: Accept-Encoding x-powered-by: PHP/7.2.34 x-turbo-charged-by: LiteSpeed` – Leb_Broth Jun 25 '21 at 14:27
  • @Leb_Broth it looks like the issue is at the server not returning a `location` header. You can validate that using the browser's dev tools under the Network tab. In that case you should find out why isn't the server returning this header. One option that I can think of is that you output something before or after you call the `header` function. try to `die();` right after the `header` call, and make sure there's no output at all before. (also make sure there isn't any blank spaces before ` – HTMHell Jun 25 '21 at 14:32
  • it seems we are looking into the POST request headers while we should look into the GET response headers. The console.log is showing the POST headers and after the console output i can see the GET response with the link I want, that is returning from the PHP header. I will try the tips in your last comment. – Leb_Broth Jun 25 '21 at 14:40