I am trying to parse the JSON string sent as ajax response from PHP file using jQuery / JavaScript.
Ajax call:
<script>
$(document).ready(function(){
var query = $("#editor").html().replace(/(<([^>]+)>)/ig,"");
$.ajax({
type: "POST",
url: "app/query_db.php",
data: "q="+query,
success: function(resp){
if(resp) {
alert("Response : "+resp);
$("#res_data").html(resp);
}
else{
alert("Response: Failed")
}
});
return false;
});
});
</script>
PHP file :
$db_conn = new db_connect();
$conn = $db_conn->connectToDB($DBHost,$DBName,$DBUser,$DBPwd);
$query_res = array();
if (fill_numeric_array_with_query_assoc($query_res, $sql_query, $conn)) { // Function to execute the query and fill the result
//do nothing
} else {
echo("Error description: " . mysqli_error($conn)); // Error message to display if failed to fetch the data
exit();
}
$data = json_encode($query_res);
echo $data;
Result: [{"uid":"admin","pwd":"xyz"},{"uid":"guest","pwd":"abc"}]
Whenever I try to parse the above JSON string I am ending up with 0 and 1 as keys and the other stuff as values. The strange thing is when I copy this result and assign it to a variable in JavaScript it's parsing the string correctly.
Don't know what's going wrong when trying to parse it as AJAX response.
Table format when parsed the JSON string as AJAX response:

Response when assigned to variable in javascript :
uid pwd
admin xyz
guest abc
0and1?