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('[\\?&]' + name + '=([^&#]*)').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}
.