简体   繁体   English

AJAX Post不发送数据?

[英]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... 我正在运行一个名为logOut()的javascript注销函数,它已经对php脚本进行了jQuery ajax调用...

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: 它调用的php函数在这里:

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. 现在,18个小时工作,但由于某种原因,每隔一段时间,POST数据将不会触发我的查询。 (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 替代文字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 替代文字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. 我知道它被跳过了两个原因,一个是我的第一个函数结束时的die(),另外两个,如果成功则会在表中注册“logOutSuccess”。

Any thoughts? 有什么想法吗? One friend says it's a janky hosting company (hostgator.com). 一位朋友说这是一家虚张声势的托管公司(hostgator.com)。 I personally like them because they are cheap and I'm a fan of cpanel. 我个人喜欢他们,因为他们便宜而且我是cpanel的粉丝。 But, if that's the case??? 但是,如果是这样的话???

Thanks in advance. 提前致谢。

-J -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. 我删除了完整的URL http://www.mydomain.com/functions.php并将其替换为本地路径functions.php,这就是诀窍。

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. 显然AJAX存在跨域ajax调用的问题,而且我不在专用服务器上,所以我想发生的事情是每隔几个小时(或几分钟)我从不同的位置点击我的脚本导致AJAX解除POST数据。

-J -J

Try enabling error reporting on the jquery $.ajax function, your code would look something like 尝试在jquery $ .ajax函数上启用错误报告,您的代码看起来像

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. 函数末尾的die()是可疑的,但我不太确定它会如何影响它。

Btw you can see http headers in FireBug's Net tab, to know whether those parameters has been sent properly. 顺便说一下,您可以在FireBug的Net选项卡中看到http标头,以了解这些参数是否已正确发送。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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