I want to use jQuery to run an asynchronous post with data to "[domain1]/first.php" on server #1 when a user on a page "[domain2]/unrelated.php" on server #2 clicks a link to "[domain2]/second.php?var1=data1?var2=data2" that's on server #2. I don't want to wait for "[domain1]/first.php" to finish or return back any response whatsoever.
- "[domain1]/first.php" is set up to process data (it also does a cURL to "[domain2]/unrelated2.php" on server #1) and not echo anything, and to finish with an "exit;" or "?>" line.
-
"[domain2]/second.php" processes the data and echoes a page.
- It works in Chrome, Opera and Firefox when it's synchronous. (but it's slow because of data processing, I want to avoid the wait after posting and before it follows the clicked link)
- It works in Chrome or Opera when it's asynchronous.
- Firefox gives a CORS error in the console for "[domain1]/first.php" when it's asynchronous:
"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [domain1]/first.php. This can be fixed by moving the resource to the same domain or enabling CORS."
I've tried adding this to the top of "[domain1]/first.php":
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
//exit(0);
}
The jQuery AJAX call on "[domain2]/unrelated.php" is:
$.ajax({
url: '[domain1]/first.php',
type: 'POST',
data: parameters
});
All three browsers used for testing are up to date. The jQuery in use is 1.7.2.
I could detect Firefox and only make it use a synchronous post, but it hardly seems satisfying considering it works asynchronously in other two browsers.
Any thoughts or suggestions?
I could post how the request and response headers look like, but I'm unable to do so right now, I'll edit them in at a later time, I apologize. From searching around, this seems to primarily be a problem of the header that server #2 sends back in response from the original post from server #1, so I assume posting those headers is critical in solving this case? Maybe I have to uncomment the commented "//exit(0);"?
Thanks for your time!