【问题标题】:Using a specific URL in XMLHttpRequest, return NetworkError: A network error occurred在 XMLHttpRequest 中使用特定的 URL,返回 NetworkError: A network error occurred
【发布时间】:2018-02-19 07:20:54
【问题描述】:
var xmlhttp = new XMLHttpRequest;
if (!xmlhttp) {
    alert ("XMLHttpRequest create error");
}
xmlhttp.open ("GET", "http://127.0.0.10/advert/123", false);
xmlhttp.onreadystatechange = function () {

    if (xmlhttp.readyState == 4) {
        console.log (xmlhttp.status);
        if (xmlhttp.status == 200) {
            console.log (xmlhttp.responseText);
        } else {
            alert ("Error");
        }
    }
};
xmlhttp.send ();

具体的网址是'//host/advert/???'。喜欢:

//127.0.0.12/advert/123

//127.0.0.12/advert/test

还有更多...

只使用这样的Url,就会出现错误。

具体的URL env是PHP+APACHE(rewrite),代码如下:

index.php

echo 123;
exit;

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

【问题讨论】:

    标签: javascript php .htaccess url-rewriting xmlhttprequest


    【解决方案1】:

    您正在混合使用同步和异步请求。

    如果您将false 作为第三个参数传递给open 方法,这意味着请求应该是同步的(为什么,顺便说一句?),那么您应该像这样处理它:

    xmlhttp.open("GET", "http://127.0.0.10/advert/123", false);
    xmlhttp.send(null);
    
    if (xmlhttp.status === 200) {
      console.log(xmlhttp.responseText);
    }
    

    补充资料:Synchronous and asynchronous requests

    【讨论】:

    • 感谢您的回答。这只是一个演示,用于测试是否有请求,即'xmlhttp.status == 200'。我发现了问题。该死的插件 Adblock = =`
    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多