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 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务安装失败
  • ¥15 基于面向对象的图书馆借阅管理系统
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥20 centos linux 7.9安装php8.2.18不支持mysqli模块的问题
  • ¥15 stata空间计量LM检验
  • ¥15 关于k8s node节点被释放后如何驱逐节点并添加新节点