Jquery get array details

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-04-26T18:06:23Z Indexed on 2010/04/26 18:23 UTC
Read the original article Hit count: 199

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JSON