dongze5043 2014-11-28 00:02
浏览 49
已采纳

从php文件中使用.getJSON检索JSON数据

I've got a problem with sending json data.

Here's my php file:

<?php
session_start();
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result_array = array();
$result = mysql_query("SELECT Reg FROM bool");

while ($row = mysql_fetch_array($result)) 
$result_array[] = $row;


echo json_encode($result_array);
mysql_close($con);
?>

And here's my js script:

$(function () {
    $(document).ready(function() {
    $.getJSON("sql_bool.php", function(data) {
    alert(data[1]);     
    });    
});    
});

I've got some bool data stored in my database, and when i try to check with an alert() function if it made it through, all i get is an alert window with

[object Object]

Any idea what's wrong?

  • 写回答

2条回答 默认 最新

  • dongxing7318 2014-11-28 00:10
    关注

    It sounds like it went through fine. When you alert in JavaScript it turns whatever you alert into a string. In this case your object as a string would be [ object object].

    You could try:

    for(var k in data[1]){ alert(k); alert(data[1][k]); }

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

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动