dongmaqiu6084 2017-08-02 01:46
浏览 62
已采纳

使用json_encode将数据从php传递到javascript问题

I am kinda lost as to why I cant get data passed
javascript

jQuery.extend({
getValues: function(url) {
    var result = null;
    $.ajax({
    url: url,
    type: 'get',
    dataType: 'json',
    async: false,
    success: function(data) {

        result = JSON.stringify(data);

    }
return result;

I have no problems with the js part the problem is the php

$sql = "SELECT name, score FROM scores ORDER BY score DESC LIMIT 10 ";
$result = mysqli_query($conn1, $sql);
if (mysqli_num_rows($result) > 0) {
$data = array(); 
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = array("name"=>$row['name'], "score"=>$row['score']);
    $post_data = json_encode(array($data));
}
echo $post_data;
}

echoing post data gives nothing if i echo post data inside while loop i get a result for every iteration of while loop. If i print_r data array i also get some kind of result. My question is what am i doing wrong?
Thank you for your answers I did bring out post data from the loop that was poorly made indeed however the real problem was that one of the names was in russian alphabet and the result from database wasnt utf-8

  • 写回答

3条回答 默认 最新

  • dsa99349 2017-08-03 18:04
    关注

    using json_last_error I found that I couldnt encode because data wasn't utf-8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵