如何使用Ajax来解决一个“NS_ERROR_ILLEGAL_VALUE”的错误? [英] How to get around a 'NS_ERROR_ILLEGAL_VALUE' error using Ajax?
问题描述
我只是写一个小Ajax框架的可重用性的小型项目,我已经打了一个问题。基本上,我得到了 NS_ERROR_ILLEGAL_VALUE
的错误,而发送请求和我不知道发生了什么。
Im just writing a small Ajax framework for re-usability in small projects and i've hit a problem. Basically i get a 'NS_ERROR_ILLEGAL_VALUE
' error while sending the request and i've no idea what is happening.
HTML页面 (修剪,但显示错误)
The HTML Page (trimmed but shows the error)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var COMPLETE = 4;
var OK = 200;
function GetXMLHttpRequestObject()
{
var XMLHttpRequestObject = false;
if(window.XMLHttpRequest)
{
if(typeof XMLHttpRequest != 'undefined')
{
try
{
XMLHttpRequestObject = new XMLHttpRequest();
}
catch (e)
{
XMLHttpRequestObject = false;
}
}
}
else if (window.ActiveXObject)
{
try
{
XMLHttpRequestObject = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e)
{
try
{
XMLHttpRequestObject = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e)
{
XMLHttpRequestObject = false;
}
}
}
else
{
XMLHttpRequestObject = false;
}
return XMLHttpRequestObject;
}
//The Main Ajax Object
function AjaxRequest(p_RequestMethod, p_DestinationURL)
{
this.XMLHttpRequestObject = GetXMLHttpRequestObject();
this.RequestedMethod = p_RequestMethod;
this.DestinationURL = p_DestinationURL;
this.XMLHttpRequestObject.open(this.RequestMethod, this.DestinationURL);
this.OnStateChange = function(Callback)
{
this.XMLHttpRequestObject.onreadystatechange = Callback;
}
this.Send = function(p_Content)
{
this.XMLHttpRequestObject.send(p_Content);
}
this.GetState()
{
return this.XMLHttpRequestObject.readyState;
}
this.GetResponseText = function()
{
return this.XMLHttpRequestObject.responseText;
}
this.GetResponseStatus = function()
{
return this.XMLHttpRequestObject.status;
}
this.GetResponseStatusText = function()
{
return this.XMLHttpRequestObject.statusText;
}
}
var Request;
function GetData()
{
Request = new AjaxRequest('POST', 'http://www.kalekold.net/ajax/Ajax.php');
Request.OnStateChange = StateChange;
Request.Send();
}
function StateChange()
{
window.alert("State: " + Request.GetState());
window.alert("Response: " + Request.GetResponseStatus());
window.alert("Response Text: " + Request.GetResponseStatusText());
if(Request.GetState() == COMPLETE && Request.GetResponseStatus() == OK)
{
Result = Request.GetResponseText();
window.alert(Result);
}
}
</script>
</head>
<body>
<form>
<textarea name="TextArea" rows="10" cols="80"></textarea><br />
<input type="button" value="Load" onClick="GetData();">
</form>
</body>
</html>
PHP文件:
The PHP File:
<?php
$XML = <<< PROLOG
<?xml version="1.0" encoding="iso-8859-1"?>
PROLOG;
$XML .= "<results>";
$XML .= "<result>";
$XML .= "<FirstName>Gary</FirstName>";
$XML .= "<SecondName>Willoughby</SecondName>";
$XML .= "<Age>35</Age>";
$XML .= "</result>";
$XML .= "<result>";
$XML .= "<FirstName>Sara</FirstName>";
$XML .= "<SecondName>Gostick</SecondName>";
$XML .= "<Age>35</Age>";
$XML .= "</result>";
$XML .= "</results>";
header("Content-Type: text/xml");
echo $XML;
?>
完整的错误:
The full error:
uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXMLHttpRequest.open]" nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)" location: "JS frame :: http://www.kalekold.net/ajax/ :: AjaxRequest :: line 63" data: no]
Line 0
我就是不能看到它会错了,任何想法?
I just can't see where it's going wrong, any ideas?
推荐答案
例外组件返回故障code:80070057(NS_ERROR_ILLEGAL_VALUE)是由一个非法值被传递到open方法的调用
The exception "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE)" is caused by an illegal value being passed into the call of open method.
翻翻你的code,我发现拼写错误:
Looking through your code I found misspelling:
this.RequestedMethod = p_RequestMethod;
this.DestinationURL = p_DestinationURL;
this.XMLHttpRequestObject.open(this.RequestMethod, this.DestinationURL);
请参阅this.RequestedMethod属性设置为p_RequestMethod和this.RequestMethod被传递到开放的方法调用。
See this.RequestedMethod property set to p_RequestMethod and this.RequestMethod being passed into the call of "open" method.
此外,而不是创建您自己的包装,我会建议使用开源 XMLHtt prequest.js - 符合标准的跨浏览器XMLHtt prequest对象实现,也解决浏览器的原生XMLHtt prequest对象实现的20虫子
Also, instead of creating your own wrapper, I would recommend using open-source XMLHttpRequest.js - Standard-compliant cross-browser XMLHttpRequest object implementation, that also fixes some 20 bugs of browser's native XMLHttpRequest object implementations.
这篇关于如何使用Ajax来解决一个“NS_ERROR_ILLEGAL_VALUE”的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!