dongti7838 2014-05-15 01:00
浏览 165
已采纳

将DBF中的UTF-8编码JSON解析为JS

I have the following column in my database with UTF-8 encoded letters:

  • chineseWords

    nǐ hǎo

console.log(value) gives: null


If I change them to non UTF-8,

  • chineseWords

    ni hao

console.log(value) gives: ni hao


If I just keep one word

  • chineseWords

console.log(value) gives: n?


jQuery:

function getData (functionToRun) {  
    $.getJSON("phpscripts.php", {"_functionToRun" : functionToRun},
        function (returned_data) {                      
            var value = returned_data.chineseWords;         
            console.log(value);
        }
    );
}

PHP:

$qry = 
    'SELECT * 
    FROM tasks
    WHERE npc_id_fk = 1';

$result = $mysqli->query($qry) or die(mysqli_error($mysqli));

while ($row = $result->fetch_assoc()) {
    echo json_encode($row);
}   

Why would the browser be outputting null and n??


EDIT: following this blog on encoding and decoding UTF-8, I tried:

console.log(decodeURIComponent(value)); //output: n?


EDIT 2:

Connect:

<?php 
$mysqli = new mysqli("localhost", "root", "", "my_db");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (Error code: " . $mysqli->connect_errno . ")... " . $mysqli->connect_error;
}
?>

Start of my phpscripts.php file:

<?php
include 'runDB.php';
mysql_set_charset($mysqli, "utf8");

index.php:

<!DOCTYPE html>
<html>
    <head>
        <title>Playground</title>
        <meta charset="UTF-8">
  • 写回答

1条回答 默认 最新

  • douzi7890 2014-05-15 01:17
    关注

    In your HTML Header add:

    <meta charset="UTF-8">
    

    In your PHP Mysql call add:

    mysql_set_charset('utf8',$connectToDBVar);
    

    after your db mysql_connect but before your query

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序