最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get around a 'NS_ERROR_ILLEGAL_VALUE' error using Ajax? - Stack Overflow

programmeradmin5浏览0评论

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.

The HTML Page (trimmed but shows the error)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".dtd"> 
<html xmlns=""> 
    <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', '.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>

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 :: / :: AjaxRequest :: line 63"  data: no]

Line 0

I just can't see where it's going wrong, any ideas?

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.

The HTML Page (trimmed but shows the error)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3/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/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>

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/ajax/ :: AjaxRequest :: line 63"  data: no]

Line 0

I just can't see where it's going wrong, any ideas?

Share Improve this question asked Oct 14, 2008 at 23:00 Gary WilloughbyGary Willoughby 52.5k43 gold badges140 silver badges204 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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.

Looking through your code I found misspelling:

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

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

See this.RequestedMethod property set to p_RequestMethod and this.RequestMethod being passed into the call of "open" method.

Also, instead of creating your own wrapper, I would remend using open-source XMLHttpRequest.js - Standard-pliant cross-browser XMLHttpRequest object implementation, that also fixes some 20 bugs of browser's native XMLHttpRequest object implementations.

This error message is one of the 'quirks' of FireFox's XMLHttpRequest object. The same issue in IE will have different symptoms.

You don't want to deal with all these quirks yourself now that there's lots of good libraries out there.

For instance in Netscape and FX calling XMLHttpRequestObject.responseText or XMLHttpRequestObject.status throws an "NS_..." error for any connection problems. IE will returns the OS network error code instead - no error thrown. If you handle this yourself you will have to build in the error handling for both.

I would remend jQuery. Prototype is also excellent.

发布评论

评论列表(0)

  1. 暂无评论