为什么我的 AJAX 调用在 Google Chrome 中失败?

发布于 2024-12-06 21:33:58 字数 856 浏览 1 评论 0原文

我很好奇为什么我的 AJAX 调用在 Google Chrome 中失败,但它在 Firefox 中工作得很好。在有人问之前,我不使用 JQuery,因为我需要访问 readyState == 3,而 JQuery 似乎没有。

我的脚本目前看起来像这样(删除了大的不必要的部分):

function fetch()
{
    main = new XMLHttpRequest();
    main.open("GET", "<?php echo anchor("thescript"); ?>", true);

    var lastResponse = '';
    var statusString = 'Step 1(of 3), please wait... ';

    main.onreadystatechange = function()
    {
        if( main.readyState == 1 ) 
        {
            alert('Fetch!');
            $("#ajax-status").html( statusString );
        }

        // If there's been an update
        if( main.readyState == 3 )
        {
        }
        if( main.readyState == 4 )
        {
        }
    };
    main.send(null);
}

它在 Firefox 中完美运行,但在 Chrome 中它甚至不会发出任何警报,因此它甚至不会进入就绪状态 1(即当您发送它时)--这看起来很奇怪......

有什么想法吗?

I am curious as to why my AJAX-call is failing in Google Chrome, it works perfectly fine in Firefox. Before anyone asks, no I'm not using JQuery because I need to have access to readyState == 3 which JQuery doesn't seem to have.

My script currently looks like this(with large unneccessary parts stripped out):

function fetch()
{
    main = new XMLHttpRequest();
    main.open("GET", "<?php echo anchor("thescript"); ?>", true);

    var lastResponse = '';
    var statusString = 'Step 1(of 3), please wait... ';

    main.onreadystatechange = function()
    {
        if( main.readyState == 1 ) 
        {
            alert('Fetch!');
            $("#ajax-status").html( statusString );
        }

        // If there's been an update
        if( main.readyState == 3 )
        {
        }
        if( main.readyState == 4 )
        {
        }
    };
    main.send(null);
}

It works perfectly in Firefox but in Chrome it doesn't even alert anything so it doesn't even get into readyState 1(which is when you send it) -- that seems rather odd..

Any ideas??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2024-12-13 21:33:58

如上所述:

在设置 .onreadystatechange 之后放置 .open() 有什么区别

是的,Ein~,它确实有所不同!我想,就绪状态现在可以正常工作了!当它发送请求时,我收到了警报,并且我还在readyState == 3中尝试了警报,它也发出了警报。然而,由于某种原因,响应似乎是空的

As noted above:

does it any difference to put the .open() after you set .onreadystatechange

And yes Ein~, it actually does a difference! The readystate's are now working properly, I think! I recieve the alert when it sends the request and I also tried an alert in readyState == 3 and it alerts that too. However, the response seems to be empty for some reason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文