Below ajax code is receiving a json array from php. Rest details i have written as comments:
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data: datastr,
cache: false,
//dataType: 'json',
success: function (arrayphp) {
//"arrayphp" is receiving the json array from php.
//below code is working where iam displaying the array directly
//This code displays the array in raw format.
$(".searchby .searchlist").append(arrayphp);
}
});
FRIENDS CONCENTRATE ON THIS SECTION.NOW I WILL MAKE U THE PROBLEM MORE CLEAR AND EXACT: 1)success function is having two code 2)one is uncommented and other is commnented 3)the commneted code works if i comment code "dataType: "json"", 4)but the uncommneted code does not work with the situation which the below code currently has
$.ajax({
type: "POST",
url: "dbtryout2_2.php",
data: datastr,
dataType: "json",
cache: false,
success: function (arrayphp) {
$.each(arrayphp, function (i, v) {
alert(i + "--" + v);
});
/*$(".searchby .searchlist").append(arrayphp);*/
},
error: function (xhr) {
console.log(xhr.responseText);
}
});
BELOW IS THE PHP CODE SNIPPET RESPONSIBLE FOR RETURNING JSON ARRAY:
$arrayphp = array();
//$result is containing the list of albums
//iam one by one taking the album names and assigning it to $row
//then from $element iam pushing the elements in $arrayphp
//after pushing all the elements iam returning the json encoded array to ajax code.
while ($row = mysql_fetch_array($result)) {
$element = $row['cat_name'];
array_push($arrayphp, $element);
}
echo json_encode($arrayphp);
}
THE ARRAY RETURNED IS A LIST OF ALBUM NAMES:
["album1", "album2", "album5", "album4", "album6", "album7", "album8", "album9", "album10", "album11"]
THE EXACT ABOVE ARRAY IS GETTING RETURNED.
WOULD ANYBODY FIGURE OUT WAT THE PROBLEM IS WITH MY CODE?
jQuery.parseJSON()
api.jquery.com/jQuery.parseJSON – Vicky Thakor Aug 31 '13 at 19:21console.log(arrayphp);
in your success method, and see what comes out in the console under developer tools. You should also add this aftersuccess
to see if you are getting an error:error: function(xhr){ console.log(xhr.responseText); }
– Jake Aug 31 '13 at 19:34