HTTPXmlRequest返回状态为0的空响应文本

HTTPXmlRequest return empty responsetext with status 0

本文关键字:响应 文本 返回 状态 HTTPXmlRequest      更新时间:2023-09-26

我遇到了与XMLHttpRequest状态0 (responseText为空)类似的情况,但我觉得这种差异足够强,它值得它自己的问题。

所以,我的HTTPXmlRequest看起来像这样:
function displayIndicator() {
  alert(loader.responseText);
}
var loader = new XMLHttpRequest();
loader.onload = displayIndicator(); 
loader.open("get", "/products", true);
loader.send();

/products在浏览器窗口加载时,或通过

curl http://localhost:3000/cart/indicator;

返回一个完整的html文档,(我使用它作为一个测试页面的时刻,最终的结束点将改变,但正确的端点行为相同的方式)。

这个页面是由节点服务器服务的,所以它使用路由器和/products应该正确地解决脚本被调用的地方,这让我怀疑这不是通常的跨域问题。

任何想法吗?

您正在执行displayIndicator函数并将其返回值(undefined)分配给loader.onload,您只需要删除括号:

loader.onload = displayIndicator;