2015-11-04 96 views
1

我必須發送日文名字通過XMLHttpRequest。但它顯示編碼問題...我的tpl頁面是utf-8字符集。通過XMLHttpRequest發送日文字符

這裏是我的代碼。

function getFormData(dno,rno) { 
    var name = document.getElementById("f_nickname").value; 
    var digNo = dno; 
    var resNo = rno; 
    var strVal = digNo + "-" + resNo; 
    stp.push(strVal); 

     var xhr = new XMLHttpRequest(); 
     if (!xhr) return false; 
     var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + name + '&diagres=' + stp; 
     xhr.open('POST', url, true); 
     xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     xhr.send(stp); 
     return true; 

}

我已經通過網址是這樣的:

http://crp.com/ajax.php?prc=diagnoses&name=大阪&diagres=0-0,1-3,2-2,3-3,4-3,5-2 

,但它顯示在Ajax.php像

http://crp.com/ajax.php?prc=diagnoses&name=ƒsƒU&diagres=0-0,1-3,2-2,3-3,4-3,5-2 

在許多方面試圖...如何解決? 在此先感謝...

+1

您是否嘗試過設置'xhr.setRequestHeader(「Accept-Charset」,「unicode-1-1; q = 0.8」);'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14。 html – JoSSte

+1

我不希望它在這裏有所作爲,但是在把它們放入URI之前,你應該通過'encodeURIComponent'運行你的字符串。 – Quentin

+0

「,但它顯示在Ajax.php像」 - 你如何測試? – Quentin

回答

3

使用encodeURIComponent在URL這樣的參數。

var url = 'ajax.php' + '?prc=' + 'diagnoses' + '&name=' + encodeURIComponent(name) + '&diagres=' + stp; 

它會導致類似網址:

http://crp.com/ajax.php?prc=diagnoses&name=%E5%A4%A7%E9%98%AA&diagres=0-0,1-3,2-2,3-3,4-3,5-2 

與Web服務器會妥善處理。

+0

謝謝..工作完美... – VJS

1

嘗試增加:

xmlHttp.setRequestHeader('Content-type', 'text/xml;charset=utf-8'); 

用途:

encodeURIComponent(name),而不是僅僅name

+0

'stp'不是一個XML文檔。爲什麼這會有所幫助?無論如何,這個問題在URL中。 – Quentin

+0

謝謝..工作完美... – VJS