Im using AJAX to request a php file that returns JSON
$.ajax({ url: 'php/invoice.php',
dataType: "json",
data: {id: 1},
type: 'post',
success: function(response) {
alert(response);
}
});
The php file im using is echoing json via json_encode:
while($row = mysqli_fetch_assoc($result))
{
$queryResults[] = $row;
}
echo json_encode($queryResults);
an example of what the php is sending back is:
[{"id":"1","firstname":"Brent","lastname":"Doe","telephone":"888888888","zip":"11111"}]
What im getting when I alert(response) is "[object Object]"
What I want is to alert the first name or any other property in the json ie. "Brent"
Iv'e tried alert(response.firstname); and it returns as undefined.
iv'e also tried to remove the dataType: "json" from the ajax request and just parse the json when it gets returned via var obj = $.parseJSON(response) and alert(obj.firstname)
I dont know what im doing wrong, I also dont know why there [ ] around the json when I use json encode. Im assume its because its returning a array from json objects which may be my problem? Im new to this and would appreciate any help or resources!
Iv'e done a couple hours of research and still cant find a answer. Thanks in advance!