我搜索了很多但仍未找到问题的原因:
当我调用我的php webservice时,我正在编码登录凭据(“PeterPan:Ützel([])ÄÄÖÖ'”)并使用UTF-8传递它们:
$.ajaxSetup({
beforeSend: function(request) {
request.setRequestHeader("Authentication", auth);
},
type: "POST",
contentType: "charset=utf-8"
});
var tok = sUsername + ':' + sPassword;
var hash = this.Base64.encode(tok);
var auth = hash;
$.ajax({
url: "php/login.php"
});
在PHP中,我使用:
解码字符串$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
$response['Encoded'] = base64_decode($headers['Authentication']);
$encoded = json_encode($response);
header("Content-Type: text/html; charset=utf-8");
echo $encoded;
当响应到达时,编码的字符串如下所示:“PeterPan:\ u00dctzel([])\ u00c4 \ u00c4 \ u00d6 \ u00d6'”
变音符号编码错误......
我做错了什么?
答案 0 :(得分:0)
我相信变音符号编码正确。但是,如果您需要打印或回显它到浏览器,那么您可能需要使用iconv()指定正确的编码
答案 1 :(得分:0)
您可以执行以下操作
javascript:
// returns PeterPan%3A%C3%9Ctzel(%5B%5D)%C3%84%C3%84%C3%96%C3%96
var value = encodeURIComponent(sUsername + ':' + sPassword);
PHP:
header('Content-Type: text/html; charset=utf-8'); // might need this
// returns PeterPan:Ützel([])ÄÄÖÖ
echo rawurldecode("PeterPan%3A%C3%9Ctzel(%5B%5D)%C3%84%C3%84%C3%96%C3%96");