使用HTML5进行跨源资源共享的问题

发布于 2024-09-13 16:41:32 字数 1354 浏览 9 评论 0原文

我是这个网站的新手,并且已经成功使用“HTML5”跨源资源共享(CORS)将数据发布到我的服务器。我最近尝试将 GET 包含在其中,因为我们试图让所有通信都不依赖于 JavaScript 库。在这样做的过程中,我遇到了一个奇怪的问题,对于 Firefox 来说似乎可以解决,但在所有 WebKit 浏览器中仍然表现不佳。本质上,任何返回 <200 或 >300 状态的内容都只是以 0 状态出现。这使得几乎不可能进行错误处理。

对于 Firefox,我可以在请求末尾放置一个随机字符串以防止它被缓存;从而至少修复了304s。然而,这对我在 Chrome/Safari 中根本不起作用。这是我的代码示例:

xhr_request: function(type,url, data, callbacks, form){
    var form = (typeof(form)!=="undefined")?form:null;
    var xhr = new XMLHttpRequest();
    var response;
    data = com.ticommunity.obj_to_string(data);
    xhr.open(type, url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("X-TxId", com.ticommunity.keyGen());
    xhr.withCredentials = true;
    xhr.send(data);
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                response = xhr.responseText;
                com.ticommunity.comm.request_callback(response, callbacks, form);
            }else{`//****this is where the 0 Status keeps coming up***`
                response = xhr.status;
                com.ticommunity.comm.request_callback(response, callbacks, form);
            }
        }
    }
}

是否有其他人遇到类似的情况并提出解决方法?我只是做了一些愚蠢的事情吗?非常感谢任何帮助。

编辑:我意识到 0 是按照规范应该发生的情况,但我真的需要能够捕获这些情况并做一些其他处理。

I am new to this site and had been successfully using the "HTML5" Cross-origin resource sharing (CORS) to POST data to my server. I just recently tried to include GETs in that as we are trying to have all of our communications be non-reliant on a JavaScript library. In doing so I have run into an odd issue that seems somewhat fixable for Firefox but is still misbehaving in all the WebKit browsers. Essentially anything that returns a status of <200 or >300 is just coming in as a status of 0. This makes it next to impossible to do error handling.

For Firefox, I am able to put a random string on the end of the request to prevent it from being cached; thereby fixing the 304s at least. This however does not work at all for me in Chrome/Safari. Here is a sample of my code:

xhr_request: function(type,url, data, callbacks, form){
    var form = (typeof(form)!=="undefined")?form:null;
    var xhr = new XMLHttpRequest();
    var response;
    data = com.ticommunity.obj_to_string(data);
    xhr.open(type, url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("X-TxId", com.ticommunity.keyGen());
    xhr.withCredentials = true;
    xhr.send(data);
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                response = xhr.responseText;
                com.ticommunity.comm.request_callback(response, callbacks, form);
            }else{`//****this is where the 0 Status keeps coming up***`
                response = xhr.status;
                com.ticommunity.comm.request_callback(response, callbacks, form);
            }
        }
    }
}

Has anyone else run into something similar and come up with a work-around? Am I just doing something stupid on my end? Any help is greatly appreciated.

EDIT: I realized the 0 is what is supposed to happen per the spec, but I REALLY need to be able to trap for these situations and do some other handling.

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

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

发布评论

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

评论(1

卖梦商人 2024-09-20 16:41:32

我还没有具体看到这个问题,但我想知道使用不同的 xhr 事件(例如 xhr.onload)是否会有更好的运气。您可以相信 onload 会在成功响应时触发,因此无需检查 xhr.status。如果您需要捕获其他内容,这里是完整的事件列表:

http://www.w3 .org/TR/XMLHttpRequest2/#events

I haven't seen this issue specifically, but I wonder if you'd have better luck using a different xhr event, such as xhr.onload. You can trust onload to fire on successful responses, so there's no need to check xhr.status. Here's a complete list of events if you need to trap on something else:

http://www.w3.org/TR/XMLHttpRequest2/#events

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