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

1 Answer 1

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");
}
Sign up to request clarification or add additional context in comments.

7 Comments

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: stackoverflow.com/a/15444439/653182
@htmhell I did it but it is redirecting now to a NULL url
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.
And what's the output you see on the console?
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.