-1

The relevant part of my code looks like this.

try
{
  $customer = \Stripe\Customer::create(array(
    'email' => $_POST['custEmail'],
    'source'  => $_POST['stripeToken'],
    'plan' => 'my_plan'
  ));
 header("Location:https://something.com"); 
  exit;
}
catch(Exception $e)
{
  //header('Location:oops.html');
    echo "no work";
  error_log("unable to sign up customer:" . $_POST['custEmail'].
    ", error:" . $e->getMessage());
}

The response from my PHP file includes this:

HTTP/1.1 302 Found
Date: Sun, 31 Jul 2016 21:13:52 GMT
Server: Apache/2.4.23 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.22
X-Powered-By: PHP/5.6.22
Location: https://something.com
Content-Length: 0
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

So, the message is coming back (I guess), but my browser doesn't redirect. What am I missing?

jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • 2
    what does "the message is coming back (I guess)" mean ? –  Jul 31 '16 at 21:21
  • @Dagon I mean that based on what the response headers from my PHP are (shown in question), my browser gets the redirect message, but doesn't do anything with it. – jonmrich Jul 31 '16 at 21:23
  • so the page is not changing to `https://something.com` ? –  Jul 31 '16 at 21:24
  • @Dagon That's correct. – jonmrich Jul 31 '16 at 21:25
  • 1
    have you checked you even reach the header call at all. `exit('HERE');` is a good test –  Jul 31 '16 at 21:26
  • 1
    @Dagon Not sure what this means: "tyoui even rich the header call" – jonmrich Jul 31 '16 at 21:26
  • @Dagon Where does `exit('HERE')` go and where would I see it return something? – jonmrich Jul 31 '16 at 21:31
  • above the header line. –  Jul 31 '16 at 21:32
  • How are you making the request? How are you establishing the HTTP response looks like the above? Have you checked that there isn't a subsequent request to the other URL (rather than just looking at what page ends up loaded in the browser window)? – Quentin Jul 31 '16 at 21:34
  • @Dagon I'm getting "HERE" back as a response if I do your test. – jonmrich Jul 31 '16 at 21:34
  • @Quentin I'm not sure I'm following. The request to PHP is Ajax using `POST`. There are no other `header` responses in my PHP. – jonmrich Jul 31 '16 at 21:35
  • @jonmrich — So what response does the Ajax handler get? Have you checked the ready state and status? – Quentin Jul 31 '16 at 21:38
  • @Quentin The response data is the full HTML code for the page I'm trying to redirect to. – jonmrich Jul 31 '16 at 21:40
  • 2
    oh its an ajax post? so why are you using header location at all? (and please mention such things in the first place) –  Jul 31 '16 at 21:47
  • Redirects don't work with XHR http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – symcbean Jul 31 '16 at 22:35

1 Answers1

3

The request to PHP is Ajax … The response data is the full HTML code for the page I'm trying to redirect to.

A location header means "The resource you were looking for is over here". It does not mean "Display the resource at this URL in the main browser window". The browser viewport won't redirect, whatever is handling the request will redirect.

The browser will follow the redirect and the response will be passed to the Ajax callback function (unless it triggers an error such as a Same Origin Policy violation).

If you want to load the page in the browser then don't use Ajax. Submit a regular form. The whole point of Ajax is to avoid loading a new document in the main view.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335