dongshi3061 2012-11-20 18:05
浏览 48
已采纳

JSON编码PHP AJAX jQuery响应错误

I am having a problem with getting back the response from my PHP code. The JavaScript file says the JSON object is of length 0.

Here is my returned JSON from my getalbums.php script:

[
  {
    "album_id": "50ab2c3db9396",
    "username": "tusik",
    "title": "testalbum",
    "cover_photo_url": "http:\/\/s3.amazonaws.com\/Looking_Glass_Images\/tusik_testalbum_testpic.jpeg"
  },
  {
    "album_id": "50ab3b75a46fe",
    "username": "tusik",
    "title": "test",
    "cover_photo_url": "http:\/\/s3.amazonaws.com\/Looking_Glass_Images\/tusik_test_test.png"
  }
]

getalbums.php

<?php

session_start();
include("db.php");

$albumsArray = array();

$username = $_GET['uid'];
//Preventing MySQL injection
$username = stripslashes($username);
$username = mysql_real_escape_string($username);

$sql = "SELECT album_id,username,title,cover_photo_url FROM albums WHERE   albums.username='$username'";
$result = mysql_query($sql) or die(mysql_error());

echo $count;

while ($row = mysql_fetch_object($result)) {

    $albumsArray[] = $row;
}

echo json_encode( $albumsArray );
?>

If I type /getalbums.php?uid=tusik I get the JSON from above so I know that page works.


albums.js

$.urlParam = function(name){
var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
return results[1] || 0;
}

var username = $.urlParam('uid');

var data = "uid=" + username;    
$.ajax({
  type: "GET",
  url: "getalbums.php",
  data: data,
  dataType: "json",
  processData: false
}).done(function(data) {
    //Do work

}).fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

The code gives me this error.

TypeError: Cannot read property 'album_id' of undefined

Can anybody see what I am doing wrong?

Edit: Fixed my problem jeroen's suggestion was partially right. {'uid':username} should of read {"uid":username}.

  • 写回答

3条回答 默认 最新

  • dounabi6295 2012-11-20 20:07
    关注

    You need to send both the key and the value to the server in you ajax call:

    $.ajax({
      type: "GET",
      url: "getalbums.php",
      data: {'uid': username},    // here
      dataType: "json",
      processData: false
    }).done(function(data) {
    

    You were just sending the data without a key, so that is why $_GET['uid'] was empty / unset and your query returned 0 rows.

    Edit: You are also invalidating your json with this line:

    echo $count;
    

    You need to make sure that only the json_encode line gets echoed / returned to the jQuery callback function so just remove it or comment it out.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题