javascript - 如何使用 Ajax 解决 'NS_ERROR_ILLEGAL_VALUE' 错误?

标签 javascript ajax

我只是在编写一个小型 Ajax 框架以在小型项目中实现可重用性,但我遇到了一个问题。基本上我在发送请求时收到“NS_ERROR_ILLEGAL_VALUE”错误,我不知道发生了什么。

HTML 页面 ( trim 但显示错误)

<!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 文件:

<?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;
?>

完整错误:

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

我只是看不出哪里出了问题,有什么想法吗?

最佳答案

异常“组件返回失败代码:0x80070057 (NS_ERROR_ILLEGAL_VALUE)”是由于调用 open 方法时传递了非法值引起的。

查看您的代码我发现拼写错误:

this.RequestedMethod = p_RequestMethod;
this.DestinationURL = p_DestinationURL;

this.XMLHttpRequestObject.open(this.RequestMethod, this.DestinationURL);

看到 this.RequestedMethod 属性设置为 p_RequestMethod 并且 this.RequestMethod 被传递到“open”方法的调用中。

此外,我建议使用开源 XMLHttpRequest.js,而不是创建自己的包装器- 符合标准的跨浏览器 XMLHttpRequest 对象实现,还修复了浏览器的 native XMLHttpRequest 对象实现的大约 20 个错误。

关于javascript - 如何使用 Ajax 解决 'NS_ERROR_ILLEGAL_VALUE' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/203161/

相关文章:

javascript - 如何在 reactjs 中使用响应 ajax 请求设置状态

javascript - 如何使用 _bsontype 属性处理来自 mongoDB 的文档

javascript - 我如何知道 XHR 请求出了什么问题?

ajax - 在 MVVM/Knockoutjs Web App 中保存更改的最佳方法?

javascript - 如何将 php/ajax 请求转为 OOP

javascript - 在 Electron/NodeJS 应用程序中捕获命令行响应

javascript - 语义 UI 弹出菜单不起作用

jquery - ajax 提交前的 HTML5 验证

php - 如何使用 AJAX 和 Recaptcha 发送表单? [拉维尔]

php - 使用 Ajax 数据逗号分隔的 SQL 查询来搜索表