简体   繁体   English

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

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

I get the following error in Firefox 31 ESR: 我在Firefox 31 ESR中收到以下错误:

Error: NS_ERROR_FAILURE: 错误:NS_ERROR_FAILURE:

Source file: 源文件:

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

Here is the same thing from Internet Explorer 11: 以下是Internet Explorer 11中的相同内容:

SCRIPT5022: InvalidStateError SCRIPT5022:InvalidStateError

Here is my script that does the call to the AJAX function: 这里是我的脚本,并调用 AJAX功能:

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

Here is my AJAX function: 这是我的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);
   }
  }
 }

Apparently Firefox and IE very vaguely think I'm doing cross site scripting ... on localhost ? 显然Firefox和IE 非常模糊地认为我在localhost上做跨站脚本...

For the URL I've tried passing absolute and relative paths: 对于URL,我尝试传递绝对路径和相对路径:

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

  2. window.location.href

  3. 'standard'

I've looked up a couple dozen pages most of which are here on Stack and the questions are all off. 我查了几十页,其中大多数都在Stack上,问题全都关闭了。

  1. I have zero intention of cross-site scripting. 我没有跨网站脚本的意图。

  2. I'm not changing the method . 没有改变method

  3. I'm not using a different protocol (all HTTP, not HTTPS). 我没有使用不同的协议(所有HTTP,而不是HTTPS)。

  4. I'm not using any subdomains. 我没有使用任何子域名。

  5. I never rely on or weaken my code with frameworks. 从不依赖或削弱我的代码框架。

  6. I do not care if the URL must be absolute or relative, I just need it to work. 不在乎 ,如果URL 必须是绝对或相对的,我需要它的工作。

  7. This is for an intranet website. 这是一个内联网网站。

  8. The server and client are both the same computer. 服务器和客户端在同一台计算机。

  9. The originating URL is formatted as: http://localhost/Client/standard/?http-query 原始URL的格式为: http://localhost/Client/standard/?http-query

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

  11. The Apache Access log shows that the request goes through and returns HTTP 200. Apache Access日志显示请求通过并返回HTTP 200。

  12. An alert() immediately after open open后立即发出alert()

  13. None of the states for onreadystatechange are logged in the console. onreadystatechange的状态都没有记录在控制台中。

  14. Not all parameters need to be set when calling the ajax function. 调用ajax函数时, 不需要设置所有参数。

What am I missing? 我错过了什么?

You're calling send() twice for the same XHR object, that's probably where the error comes from, as that would trigger an InvalidStateError as the object is not open anymore, it's already sending. 你正在为同一个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

        ..........

Also, you should generally url encode the data you're sending 此外,您通常应该对要发送的数据进行url编码

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

All together 全部一起

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