gpt4 book ai didi

javascript - 通过 XMLHttpRequest POST HTML

转载 作者:行者123 更新时间:2023-11-30 23:43:25 25 4
gpt4 key购买 nike

我正在尝试通过 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);

最佳答案

以下内容在 Firefox 和 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>

也许您只是忘记声明 xmlhttp 并为异步状态回调添加监听器?

<小时/>

另请参阅this SO question有关以跨浏览器方式获取 XMLHttpRequest 对象的更多信息。

<小时/>

以下是在 Firefox 中通过 FireBug 在 POST 请求中发送的内容:

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

所以你有type,它等于search,还有content,它是HTTP编码的body上面的 HTML 与编程完全一样。所以它似乎按预期工作......

关于javascript - 通过 XMLHttpRequest POST HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com