jQuery的:UTF-8不工作与Ajax请求 [英] jQuery: UTF-8 not working with ajax request
问题描述
我有使用php文件 jQuery.ajax()
来抓住从另一个PHP文件中的一些数据到一个div。
jQuery.ajax({
键入:POST,
编码:UTF-8,
数据类型:HTML,
的contentType:text / plain的;字符集= UTF-8,
网址:/path/data.php,
成功:函数(MSG){
。jQuery的('#dataBox)HTML(MSG);
}
});
我的问题是,如果我有一些特殊的字符,我通过AJAX获取数据,如氧化铝,然后我得到的问号的黑色钻石 - 马克。如果我在浏览器中打开外部文件时,它的工作原理。如果我把一些特殊字符的主页上,它的工作原理。
一些简单的code:
data.php:
$库MySQLi =新库MySQLi(本地主机,用户名,通,DB);
$ mysqli-> set_charset(UTF8);
$ mysqli->查询(SET GLOBAL TIME_ZONE ='+00:00');
$语句= $库MySQLi - > prepare(选择GROUP_CONCAT(sometext)AS MYTEXT从`mytable`);
$语句 - >执行();
$结果= selectResults($语句);
$语句 - >关闭();
$ mysqli的 - >关闭();
selectResults功能:
函数selectResults($语句)
{
$参数=阵列();
$结果=阵列();
$元= $ stmt-> result_metadata();
而($字段= $间 - > fetch_field()){
$参数[] =&放大器; $行[$现场>名称];
}
call_user_func_array(阵列($语句,'bind_result'),$参数);
而($ stmt->取()){
$ X =阵列();
的foreach($行作为$关键=> $ VAL){
$ X [$关键] = $ VAL;
}
$结果[] = $ X;
}
返回$结果;
}
data.php:
的foreach($结果$结果){
$ textArray =爆炸(',',$结果['MYTEXT']);
}
的foreach($ textArray为$文字){
回声($文本);
}
很抱歉,但如果你得到的单向code替换字符,然后输入已跨preTED为UTF-8,但原始字节输入的不匹配的UTF-8,IE它不是UTF-8。
如果你直接访问该页面时,看到正确的字符,那么它一定是那些阿贾克斯参数强制的数据,实际上是在其他一些编码UTF-8跨pretation,默认为Windows-1252的浏览器。
发布的PHP code和/或数据的原始字节。
I have a php file which uses jQuery.ajax()
to grab some data from another php file into a div.
jQuery.ajax({
type: 'POST',
encoding:"UTF-8",
dataType:"html",
contentType: "text/plain; charset=UTF-8",
url: '/path/data.php',
success: function(msg) {
jQuery('#dataBox').html(msg);
}
});
My problem is that if I have some "special" characters in the data I'm getting through ajax such as åäö then I get the "question mark in a black diamond"-mark. If I open the external file in the browser it works. If I put some special characters on the main page, it works.
Some simplified code:
data.php:
$mysqli = new mysqli("localhost", "username", "pass", "db");
$mysqli->set_charset("utf8");
$mysqli->query("SET GLOBAL time_zone = '+00:00'");
$stmt = $mysqli -> prepare("SELECT GROUP_CONCAT(sometext) AS mytext FROM `mytable`");
$stmt -> execute();
$results = selectResults($stmt);
$stmt -> close();
$mysqli -> close();
selectResults function:
function selectResults($stmt)
{
$parameters = array();
$results = array();
$meta = $stmt->result_metadata();
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $parameters);
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
return $results;
}
data.php:
foreach($results as $result){
$textArray = explode(',', $result['mytext']);
}
foreach($textArray as $text){
echo($text);
}
Sorry but if you get the unicode replacement character �, then the input has been interpreted in UTF-8 but the raw bytes of the input did not match UTF-8, I.E. it was not UTF-8.
If you see the correct characters when visiting the page directly, then it must be those ajax parameters forcing UTF-8 interpretation on data that is actually in some other encoding, the default is Windows-1252 for browsers.
Post the php code and/or the raw bytes of the data.
这篇关于jQuery的:UTF-8不工作与Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!