繁体   English   中英

XMLHttpRequest()使用绝对OR相对路径为localhost发送NS_ERROR_FAILURE

[英]XMLHttpRequest() send NS_ERROR_FAILURE for localhost with absolute OR relative path

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

错误:NS_ERROR_FAILURE:

源文件:

http://localhost/Example/scripts/index.js行: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. 'standard'

我查了几十页,其中大多数都在Stack上,问题全都关闭了。

  1. 我没有跨网站脚本的意图。

  2. 没有改变method

  3. 我没有使用不同的协议(所有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后立即发出alert()

  13. onreadystatechange的状态都没有记录在控制台中。

  14. 调用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);
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM