简体   繁体   中英

AJAX call to PHP script not working in IE

What worked:

My code works across all browsers now and looks like this :

window.onbeforeunload = function(){
   $.ajaxSetup({
   async: false
});


 $.post('./archive_onBeforeUnload.php?', {contact_id: contact_id}, function(){
 }).error(function(xhr, textStatus, error){
    console.log(xhr.statusText);
    console.log(textStatus);
    console.log(error);
    }); 
 }

Setting async to be false within $.post() didn't seem to be enough for it to work for me. I had to put that setting in $.ajaxSetup for it to work across all browsers.


My project is a sort of chat program that I created using HTML and PHP. The issue that I'm having is that if the person closes the window using the X button at the top, there is some PHP that I need to run in order for my program to still run properly. So I did some searching and ended up with this bit of code in:

window.onbeforeunload = function(){
    $.post('./archive_onBeforeUnload.php?', {contact_id: contact_id}, function(){
    }).error(function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    });

}

This calls the archive_beforeUnload.php and runs the little amount of PHP that I need it to. In FireFox, Safari, and Chrome this runs just fine but in IE I seem to have an issue. There doesn't seem to be enough time for IE to actually fully complete the call which messes up the rest of my program a bit. A way that I have around this is to use add a return statement like so:

window.onbeforeunload = function(){
    $.post('./archive_onBeforeUnload.php?', {async: 'true', cache: 'false', contact_id: contact_id}, function(){
    }).error(function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    });
    return 'I need to fix the timer here.  Without this it doesn\'t work in IE';
}

This seems to give IE enough time to actually complete the AJAX call. The thing is, it creates a confirmation window before the window closes every single time, which will get annoying for the user. Is there some way that I can get this working in IE without the need for the return statement? I've looked around the web quite a bit but have yet to find something that works for me.

Hopefully I've been clear enough in my issue.

Edit:

Thought I should maybe add my PHP script:

<?php
session_start();
require_once('client_init.php');

$sqlRequeue = 'UPDATE S2G_CHAT_TRAVELLER_QUEUE SET';
$sqlRequeue .= ' isOpen = 1';
$sqlRequeue .= ' WHERE contactID='.$contact_id;
$resRequeue = $conn_dd->query($sqlRequeue);
$resRequeue->close;

$_SESSION['s2g_chat_messages_shown'] = $default_messages_shown;
?>

I think the problem is that $.post() is asynchronous, so the function returns before it completes.

It looks like you can use the $.async() function to do the same call synchronously .

EDIT: Or you can change the default AJAX settings to be synchronous .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM