javascript - XMLHttpRequest() 为具有绝对或相对路径的本地主机发送 NS_ERROR_FAILURE

标签 javascript ajax localhost

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

Error: NS_ERROR_FAILURE:

Source file:

http://localhost/Example/scripts/index.js Line: 18

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

SCRIPT5022: InvalidStateError

这是调用 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. '标准'

我查了几十页,其中大部分都在 Stack 上,问题都没有了。

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

  2. 不会更改方法

  3. 我没有使用不同的协议(protocol)(都是 HTTP,不是 HTTPS)。

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

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

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

  7. 这是一个内联网网站。

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

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

  10. 目标 URL 是 http://localhost/Client/standard/

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

  12. open

  13. 之后立即发出 alert()
  14. 控制台中没有记录 onreadystatechange 的任何状态。

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

我错过了什么?

最佳答案

您正在为同一个 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);
            }
        }
    }
}

关于javascript - XMLHttpRequest() 为具有绝对或相对路径的本地主机发送 NS_ERROR_FAILURE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29631499/

相关文章:

javascript - 获取 jQuery 的 $(selector).each() 'element' 参数与 $(this) 之间的区别

javascript - 图像轮播 onmouseover data-slide-to

javascript - 更改 switch 语句以包含 mp4 和 ogg 文件

javascript - 如何从action中调用reducer中存储的变量?

javascript - 表单验证不适用于 ProgressButton js

javascript - 为什么通过 AJAX 下载 zip 文件需要这么长时间?

ssl - Curl (35) SSL 连接错误

javascript - AJAX只保存输入的第一个字母

localhost - 主机 localhost 是否始终可用于自己的系统?

php - 48)地址已在使用:AH00072:make_sock:无法绑定(bind)到地址[::]:80