douya2007 2012-01-19 14:00
浏览 39
已采纳

.getJSON .each()不解析JSON数据[关闭]

I am querying a PHP page (not on an external site) using $.getJSON which will return a MySQL result set, I have used json_encode() to format the result set to a JSON string. The problem is I get the data back but when I try to set the values of some hidden fields nothing happens. I had this working but now it's not. I must have changed something without realizing it. Here's the code:

jQuery:

$.getJSON('includes/Lookup.php', {locationType:$('#location').val()}, function(data) {

var valA = $('#valA');
var valB = $('#valB');
var valC = $('#valC');

$.each(data, function(index, data) {
     valA.val(data.field_valueA);
     valB.val(data.field_valueB);
     valC.val(data.field_valueC);
 });
});

Lookup.php:

<?php
$db = new MySqlDB();

if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {

if(isset($_GET['locationType'])){

    $locationType = $_GET['locationType'];
    $db->where('id', $locationType);

    $sql = "SELECT field_valueA, field_valueB, field_valueC FROM table";
    $results = $db->query($sql);

    echo json_encode($results);

} else {
    echo 'not set';
}
}

This is what I get in Firebug:

<??> {"field_valueA":2,"field_valueB":3.12,"field_valueC":12345}

I don't know why <??> is included. I am doing this exact call in two different places, on two different tables (neither are working) this is what I am getting from the other call:

<??> [{"field_valueA":905,"field_valueB":18.180000305176,"field_valueC":16453}]

If anyone has an idea as to what is going on I'd appreciate it.

EDIT: Now it seems as if ALL my ajax responses throughout my application are being prefixed with <??>. They all worked before and now they are not, I have no idea what is going on.

  • 写回答

3条回答 默认 最新

  • du5591 2012-01-20 16:28
    关注

    Solved! I am embarrassed to say that I had a typo in my code, it's very easy to miss an extra <?. Thanks for your suggestions.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。