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

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

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)