[英]XMLHttpRequest() send NS_ERROR_FAILURE for localhost with absolute OR relative path
我在Firefox 31 ESR中收到以下错误:
错误:NS_ERROR_FAILURE:
源文件:
以下是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,我尝试传递绝对路径和相对路径:
document.getElementsByTagName('base')[0].href+'standard/'
window.location.href
'standard'
我查了几十页,其中大多数都在Stack上,问题全都关闭了。
我没有跨网站脚本的意图。
我没有改变method
。
我没有使用不同的协议(所有HTTP,而不是HTTPS)。
我没有使用任何子域名。
我从不依赖或削弱我的代码框架。
我不在乎 ,如果URL 必须是绝对或相对的,我只需要它的工作。
这是一个内联网网站。
服务器和客户端都在同一台计算机。
原始URL的格式为: http://localhost/Client/standard/?http-query
目标URL是http://localhost/Client/standard/
。
Apache Access日志显示请求通过并返回HTTP 200。
open
后立即发出alert()
onreadystatechange
的状态都没有记录在控制台中。
调用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.