tags:

views:

25

answers:

1

hello all i want post page body via XmlHttpRequest

        var params = "type=search" + "&content="+encodeURIComponent(document.getElementsByTagName("body")[0].innerHTML);
        xmlhttp.open("POST", "/service/p.aspx", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length); 
        xmlhttp.send(params);

but its not working .

A: 

The following works fine for me in Firefox and IE8:

<html>
    <body>
        <script type="text/javascript">
// MAYBE FORGOT THIS PART?
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4)
    {
        document.write(xmlhttp.responseText);
    }
}

// THIS PART IS EXACTLY LIKE YOURS
var params = "type=search" + "&content="+encodeURIComponent(document.getElementsByTagName("body")[0].innerHTML);
xmlhttp.open("POST", "/service/p.aspx", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length); 
xmlhttp.send(params);
        </script>
    </body>
</html>

Perhaps you just forgot to declare xmlhttp and add a listener for the asynchronous status callback?


Also, see this SO question for more information about getting an XMLHttpRequest object in a cross-browser fashion.


Here is what is being sent in the POST request via FireBug in Firefox:

type=search&content=%0A%20%20%20%20%20%20%20%20%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%2F%2F%20MAYBE%20FORGOT%20THIS%20PART%3F%0Avar%20xmlhttp%20%3D%20new%20XMLHttpRequest()%3B%0Axmlhttp.onreadystatechange%3Dfunction()%0A%7B%0A%20%20%20%20if%20(xmlhttp.readyState%3D%3D4)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20document.write(xmlhttp.responseText)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20THIS%20PART%20IS%20EXACTLY%20LIKE%20YOURS%0Avar%20params%20%3D%20%22type%3Dsearch%22%20%2B%20%22%26content%3D%22%2BencodeURIComponent(document.getElementsByTagName(%22body%22)%5B0%5D.innerHTML)%3B%0Axmlhttp.open(%22POST%22%2C%20%22%2Fservice%2Fp.aspx%22%2C%20true)%3B%0Axmlhttp.setRequestHeader(%22Content-type%22%2C%20%22application%2Fx-www-form-urlencoded%22)%3B%0Axmlhttp.setRequestHeader(%22Content-length%22%2C%20params.length)%3B%20%0Axmlhttp.send(params)%3B%0A%20%20%20%20%20%20%20%20%3C%2Fscript%3E

So you have type which equals search, and content, which is the HTTP-encoded body of the HTML above, exactly as programmed. So it seems to be working as intended...

jdmichal
hello jdmical, thnak you for your help , i declared the xmlhttp sorry for not include it in my question . i jus cant send the html page body content but i can send any thing else . thank you
fligant
@fligant I added the actual POST message to the answer. Please check.
jdmichal
thank you jsmical it's worked i don't know whay!!! but it work :) thank you for your hellp
fligant