-1

I have a PHP script on my server that needs to be run from my clients websites using Javascript in a plain HTML page. After the script is run the HTML page will redirect. The problem is that sometimes the script doesn't run before the redirect happens.

This is the code I am using...

$.ajax({
     async: false,
     type: 'GET',
     url: 'the_URL_of_the_PHP_on_my_server.php',
     success: function(data) {
     }
});

window.location="the_URL_for_the_redirect";

The PHP script on my server is to track hits/sales etc. Is there are way I can force the page to wait for the script to complete before the page redirect.

The HTML page and the PHP page are on different servers. Also, the HTML page is being used on lots of different websites, so I can't give them all permission to access my server. I'm not sure if that's causing a problem or not.

I don't need any information back from the PHP script I just need it to run.

Thank you.

Steve Hall
  • 113
  • 1
  • 7

2 Answers2

4

The success function runs when you get a response (unless it was an error, in which case the error function you haven't defined would run).

If you want some code to run after you get a response, put it inside those functions instead immediately after the code which sends the request.


That said: The point of Ajax is to talk to the server without leaving the page. If you are going to go to a different page as soon as you have a response, then don't use Ajax. Use a regular link or form submission and then having an HTTP redirect as the response.

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

This is normal, that this situation happens.

because $.ajax is async and won't wait till success method

change your code to

$.ajax({
     async: false,
     type: 'GET',
     url: 'the_URL_of_the_PHP_on_my_server.php',
     complete: function(data) {
          window.location="the_URL_for_the_redirect";
     }
});

UPDATED

changed function success to complete difference is => complete will be called not matters what happened with request


UPDATE 2

think about @Quentin variant by html redirects.

xAqweRx
  • 1,236
  • 1
  • 10
  • 23
  • Thank you. I'm changing this now, I'll test and let you know how it goes – Steve Hall May 10 '16 at 12:02
  • @SteveHall yes, you have. But this is a problem with request from one website to antoher. So you should think about https://en.wikipedia.org/wiki/Cross-site_request_forgery – xAqweRx May 10 '16 at 12:07
  • I tried this change but it gives me the following error... XMLHttpRequest cannot load PHP_PAGE. Origin HTML_PAGE is not allowed by Access-Control-Allow-Origin. Both pages are on different servers, so could this be the problem. Is there a way around this. I don't need any data back from the PHP script to the HTML page, I just need the script to run. – Steve Hall May 10 '16 at 12:10
  • The PHP script is on my server, and I need to allow it to be triggered from my customers websites. Their websites are just plain HTML. Is this possible? My customers are complete newbies that just copy and paste the code I provide into their page. – Steve Hall May 10 '16 at 12:13
  • @SteveHall try the code from answer ( changed success to complete ) + grant this request on your side => https://en.wikipedia.org/wiki/Cross-site_request_forgery – xAqweRx May 10 '16 at 12:16
  • I've changed the code over, I have no idea how to grant the request. I read through the link you posted but it doesn't make sense to me. That's all way above my level of understanding. I am guessing I need to add some code to the PHP page to allow it to be triggered from a different server? – Steve Hall May 10 '16 at 12:25
  • @SteveHall check if JS code works first. What kind of server do you use? – xAqweRx May 10 '16 at 12:31
  • The code seems to work fine. The PHP page is to track hits and sales and if I refresh the HTML page (after deleting cookies) it does add to the tracking so it does trigger the code. But it returns the error and then won't do the redirect. It's a shared server running on linux, using cpanel. – Steve Hall May 10 '16 at 12:44
  • @SteveHall add to your php code on start : header("Access-Control-Allow-Origin: http ://client.site.com"); – xAqweRx May 10 '16 at 12:53
  • please read answer here : http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-ajax/17299796#17299796 – xAqweRx May 10 '16 at 12:56
  • Thanks! That seems to be what I needed :) I just tried it with header("Access-Control-Allow-Origin: *"); and it works fine, but I guess this isn't a very secure thing to do! There are hundreds of client sites all on different servers that would need to run this script so I imagine having all of the domain names on a database and using an include on the php script would be the best way to do this? – Steve Hall May 10 '16 at 13:10
  • @SteveHall kind of :) – xAqweRx May 10 '16 at 13:11