Jquery get array details
jquery, json, php
Solution
You're missing `dataType: "json"`:
$.ajax({
type: "POST",
url: "myURL.php",
dataType: "json",
data: {username: username, password: password},
success: function(results) {
//THIS IS WHERE THE PROBLEM IS
alert('Hi '+results.name); //Should be "Hi Basil Fawlty"
}
});
Another (less verbose) alternative is `jQuery.getJSON` if you know you are getting JSON.
Problem
I'm having trouble getting a response back from a Jquery ajax call... (It's a script to authenticate a user, and needs to return their name and user ID. My understanding was that I could encode it as JSON and get the data in the format below. It is returning an error of "undefined" for the alert(). The javascript ``` $.ajax({ type: "POST", url: "myURL.php", data: {username: username, password: password}, success: function(results) { //THIS IS WHERE THE PROBLEM IS alert('Hi '+results.name); //Should be "Hi Basil Fawlty" } }); ``` The PHP (myURL.php) ``` //This comes from a SQL call that returns the following name json_encode(array( 'id'=>1, 'name'=>'Basil Fawlty' )); ``` Any help or ideas on where I am going wrong would be greatly appreciated! Thanks. Solution: The solution was adding a dataType.