简体   繁体   中英

AJAX Post Not Sending Data?

I can't for the life of me figure out why this is happening.

This is kind of a repost, so forgive me, but I have new data.

I am running a javascript log out function called logOut() that has make a jQuery ajax call to a php script...

function logOut(){
    var data = new Object;
    data.log_out = true;
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/functions.php', 
        data: data,
        success: function() {
             alert('done');
        }
    });
}

the php function it calls is here:

if(isset($_POST['log_out'])){ 
    $query = "INSERT INTO `token_manager` (`ip_address`) VALUES('logOutSuccess')"; 
    $connection->runQuery($query); // <-- my own database class...
    // omitted code that clears session etc...
    die();
}

Now, 18 hours out of the day this works, but for some reason, every once in a while, the POST data will not trigger my query. (this will last about an hour or so). I figured out the post data is not being set by adding this at the end of my script...

$query = "INSERT INTO `token_manager` (`ip_address`) VALUES('POST FAIL')"; 
$connection->runQuery($query);

So, now I know for certain my log out function is being skipped because in my database is the following data:

alt text http://img535.imageshack.us/img535/2025/screenshot20100519at125h.png

if it were NOT being skipped, my data would show up like this:

alt text http://img25.imageshack.us/img25/8104/screenshot20100519at125.png

I know it is being skipped for two reasons, one the die() at the end of my first function, and two, if it were a success a "logOutSuccess" would be registered in the table.

Any thoughts? One friend says it's a janky hosting company (hostgator.com). I personally like them because they are cheap and I'm a fan of cpanel. But, if that's the case???

Thanks in advance.

-J

Ok, for those interested.

I removed the full URL http://www.mydomain.com/functions.php and replaced it with the local path functions.php and that did the trick.

Apparently AJAX has issues with cross domain ajax calls and I'm not on a dedicated server, so I imagine what's happening is every couple hours (or minutes) I am somehow hitting my script from a different location causing AJAX to dismiss the POST data.

-J

Try enabling error reporting on the jquery $.ajax function, your code would look something like

function logOut(){
    var data = new Object;
    data.log_out = true;
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/functions.php', 
        data: data,
        success: function() {
             alert('done');
        },
       error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert(textStatus+" - "+errorThrown);
       }
    });
}

See if that sheds light on your situation.

I have a strong feeling that it's more of a server side issue rather than the client's.

The odd thing is that you see the problem for a period of time. If the client works at all, then at the minimum refreshing the page or restarting the browser should fix it.

The die() at the end of the function is suspicious, but I am not quite sure how it will affect it.

Btw you can see http headers in FireBug's Net tab, to know whether those parameters has been sent properly.

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