【问题标题】:XMLHttpRequest() send NS_ERROR_FAILURE for localhost with absolute OR relative pathXMLHttpRequest() 使用绝对或相对路径为 localhost 发送 NS_ERROR_FAILURE
【发布时间】:2023-03-06 22:22:02
【问题描述】:

我在 Firefox 31 ESR 中收到以下错误:

错误:NS_ERROR_FAILURE:

源文件:

http://localhost/Example/scripts/index.js 线路:18

这是来自 Internet Explorer 11 的相同内容:

SCRIPT5022:无效状态错误

这是我调用 AJAX 函数的脚本:

ajax('post','standard/','part='+p+'&price='+q,'sequel_sub_object_key');

这是我的 AJAX 函数:

function ajax(method,url,param_id_container_pos,id_container,id_focus,sequel)
{
 var methods = Array('get','head','post');

 if (window.XMLHttpRequest && in_array(method,methods))
 {
  xhr = new XMLHttpRequest();
  xhr.open(method,url,true);//Error triggers here for localhost.

  if (method=='post')
  {
   xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
   xhr.send(param_id_container_pos);
  }

  xhr.send(null);

  xhr.onreadystatechange = function()
  {console.log('xhr.readyState = '+xhr.readyState);
   if (xhr.readyState=='4')
   {
    alert('xhr.responseText = '+xhr.responseText);
   }
  }
 }

显然 Firefox 和 IE 非常模糊认为我在 localhost 上执行跨站点脚本...?

对于我尝试传递绝对和相对路径的 URL:

  1. document.getElementsByTagName('base')[0].href+'standard/'

  2. window.location.href

  3. 'standard'

我已经查阅了几十页,其中大部分都在 Stack 上,所有问题都已关闭。

  1. 我对跨站脚本编写的意图为零。

  2. 更改method

  3. 我没有使用其他协议(全是 HTTP,不是 HTTPS)。

  4. 我没有使用任何个子域。

  5. 从不使用框架依赖或削弱我的代码。

  6. 我确实关心 URL 必须是绝对的还是相对的,我只是需要它工作。

  7. 这是一个内网网站。

  8. 服务器和客户端都是同一台计算机。

  9. 原始 URL 格式为:http://localhost/Client/standard/?http-query

  10. 目标网址是http://localhost/Client/standard/

  11. Apache Access 日志显示请求通过并返回 HTTP 200。

  12. 紧跟在open之后的alert()

  13. onreadystatechange 的所有状态均未记录在控制台中。

  14. 调用ajax函数时,并非所有参数都需要设置。

我错过了什么?

【问题讨论】:

  • readyState 是一个数字,而不是一个字符串

标签: javascript ajax localhost


【解决方案1】:

您为同一个 XHR 对象调用了两次 send(),这可能是错误的来源,因为这将触发 InvalidStateError,因为该对象不再打开,它已经在发送。

function ajax(method, url, param_id_container_pos, id_container, id_focus, sequel) {
    ....

        if (method == 'post') {
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.send(param_id_container_pos); // you're sending here
        }

        xhr.send(null); // and then again here, which triggers an error

        ..........

此外,您通常应该对要发送的数据进行 url 编码

var data = 'part=' + encodeURIComponent(p) +'&price=' + encodeURIComponent(q);
ajax('post', 'standard/', data, 'sequel_sub_object_key');

大家一起

var data = 'part=' + encodeURIComponent(p) +'&price=' + encodeURIComponent(q);

ajax('post', 'standard/', data, 'sequel_sub_object_key');

function ajax(method, url, param_id_container_pos, id_container, id_focus, sequel) {
    var methods = ['get', 'head', 'post'];

    if (window.XMLHttpRequest && methods.indexOf(method) != -1) {
        var xhr = new XMLHttpRequest();

        xhr.open(method, url, true);

        if (method == 'post') {
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.send(param_id_container_pos);
        } else {
            xhr.send(null);
        }

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert('xhr.responseText = ' + xhr.responseText);
            }
        }
    }
}

【讨论】:

  • 需要几分钟以确保这不是发布错误。
  • 是的,错过了。我只是将它移到它自己的 GET 请求条件中。我得到了其他建议,是的,在我的较旧/成熟的系统上,它都已正确编码。谢谢!
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2016-11-09
  • 2012-09-23
  • 2014-03-14
  • 1970-01-01
  • 2011-07-09
相关资源
最近更新 更多