2013-05-08 81 views
0

我在使用AJAX發送整頁源代碼時遇到麻煩。我試過用escape(),encodeURI(),encodeURIComponent()轉義內容 - 所有這些都無法發送utf8字符。JavaScript通過AJAX發送完整HTML源代碼,UTF8問題

我的代碼是:

var http = new XMLHttpRequest(); 
var send = params='html=' + encodeURIComponent(document.documentElement.outerHTML).replace('+',' '); 
http.open('POST','submitsource.php',true); 
http.setRequestHeader('Content-type','application/x-www-form-urlencoded; charset=UTF-8'); 
http.onreadystatechange=function(){if(http.readyState==4&&http.status==200){ 
    alert('ok!'); 
}}; 

http.send(params); 

回答

0

設置你的內容類型爲「text/html的」,並讀取服務器上的整個後數據爲html

1

您可以使用標籤元:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" > 
0

問題實際上是PHP將響應寫入文件。我必須將UTF-8簽名放在我的內容前面才能寫入,而不會弄亂字符

file_put_contents('contents.txt', "\xEF\xBB\xBF".$_POST['html']);