xmlHttp string POST Headers问题

时间:2009-12-21 21:13:43

标签: php javascript ajax xmlhttprequest

我正在尝试通过XmlHttp方法传递一个字符串。我来告诉你代码:

HTML

<div id="greetings">
                    You are voting out <b style="color: #00b0de;" id=roadiename></b>. Care to explain why?<br/><br/>
                    <textarea name="textarea" id="comment" cols="38" rows="7"></textarea><br>
                    <a href="#" id="postmsg" onclick='getMsg("#comment.val()")' ><img src="images/submit.gif" style="border: none; float:right; padding-top: 10px;padding-right: 10px;"/></a>
                </div>

的JavaScript

function getMsg(msg)
{
    msgBox = msg;
}

Core.addEventListener(submit, "click", function(){Slide.send();});

发送功能

send: function()
    {   
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request");
            return;
        }

        var url="user_submit.php",
        data="vote="+value+"&sid="+Math.random();
        xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
        xmlHttp.setRequestHeader("Content-length", data.length);
        xmlHttp.open("POST",url,true);
        xmlHttp.send( data );

        function stateChanged()
        {
          if (xmlhttp.readyState==4)
          {
          document.getElementById("greetings").innerHTML=xmlhttp.responseText;
          }
        }

        function GetXmlHttpObject()
        {
        var objXMLHttp=null;
        if (window.XMLHttpRequest)
          {
          objXMLHttp=new XMLHttpRequest();
          }
        else if (window.ActiveXObject)
          {
          objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        return objXMLHttp;
        }
    },
完成所有操作后

,这是Firebug显示的错误:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.setRequestHeader]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: http://localhost/roadies/JS/script.js :: anonymous :: line 96"  data: no]

Line 0

2 个答案:

答案 0 :(得分:1)

修改

你必须致电open;在使用setRequestHeaders

之前

http://localhost/roadies/JS/script.js的第96行实际上是什么?

答案 1 :(得分:0)

W3C specWikipedia,HTTP标头实际上是“Content-Type”和“Content-Length”(请注意大写的第二个单词)。改变案例会有所作为吗?