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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊