需要向服務器發送一個頁面的HTML,以檢查一些東西
。
但是--在控制臺中得到錯誤
$(window) 。 on('load', function(>/span>) {
var str = document.documentElement.innerHTML; //does not work
//改為var str = 'lorem'; - 此項作業。
console.log(typeof(str)); // string
console.log(str); //work in both cases.
$.post('check. php', {ht: str}, function(data){ //錯誤行。
console.log(資料)。
});
});
check.php
$str = $_POST['ht'/span>]。
echo $str;
console
POST https://example.com/check.php 403
整個html文本大約是7KB
。
那么,發送html(作為字串)有什么問題呢
uj5u.com熱心網友回復:
考慮一下下面的例子:
https://jsfiddle.net/Twisty/zu074t8x/
HTML
<div id="foo">Hello World! < /div>
jQuery
$(function(/span>) {
var ht = $("body").prop("outerHTML") 。
console.log("sending", ht)。
$.ajax({
url: "/echo/html/"。
method: "POST"。
data: {
html: ht
},
success: function(results) {
console.log("Getting", results) 。
}
});
});
這將獲得Document Body元素,并創建一個外層HTML元素的字串。
對它進行編碼可能是件好事,但它應該可以作為一個字串發送。你可能想根據需要將選擇器調整為$("html")或$(dopcument)。
我在Cosnole中看到以下內容:
發送
<body>
<div id="foo">Hello World! </div>
<script type="text/javascript"/span>> //<! [CDATA[]
$(function() {
var ht = $("body").prop("outerHTML") 。
console.log("Sending", ht)。
$.ajax({
url: "/echo/html/"。
method: "POST"。
data: {
html: ht
},
success: function(results) {
console.log("Getting", results) 。
}
});
});
//]]></script>
<script>/span>
//告訴嵌入的父框架內容的高度。
if (window. parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame"/span>, {
height: document.body.getBoundingClientRect().height。
slug。""。
}], "*")
}
//總是覆寫window.name,以防用戶試圖手動設定它。
window.name = "result"。
</script>
</body>
獲得
<body>
<div id="foo">Hello World! </div>
<script type="text/javascript"/span>> //<! [CDATA[]
$(function() {
var ht = $("body").prop("outerHTML") 。
console.log("Sending", ht)。
$.ajax({
url: "/echo/html/"。
method: "POST"。
data: {
html: ht
},
success: function(results) {
console.log("Getting", results) 。
}
});
});
//]]></script>
<script>/span>
//告訴嵌入的父框架內容的高度。
if (window. parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame"/span>, {
height: document.body.getBoundingClientRect().height。
slug。""。
}], "*")
}
//總是覆寫window.name,以防用戶試圖手動設定它。
window.name = "result"。
</script>
</body>
uj5u.com熱心網友回復:
幾天前我也遇到了類似的問題,解決辦法是用javascript函式encodeURI()對字串進行編碼
var text = encodeURI("<div>test</div><b>testing</b>");
然后在php中你用
解碼$html = urldecode($_POST['ht']);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/306719.html
標籤:
下一篇:使用JQuery改變CSS(影片
