tags:

views:

122

answers:

5

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.

+4  A: 

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.

Vivin Paliath
Thanks for such a quick reply!
Matt
+1  A: 

Make sure to set your dataType to JSON so you get the response object in the success method, like this:

$.ajax({
 type: "POST",
 url: "myURL.php",
 dataType: "json",
 data: {username: username, password: password},
 success: function(results) {
  alert('Hi '+results.name);
 }
});

Details for dataType can be found here.

Alternatively, you can do this:

$.getJSON( "myURL.php", {username: username, password: password}, 
  function(results) {
    alert('Hi '+results.name);
});
Nick Craver
Thanks for the link to the dataType and the info on getJSON.
Matt
+1  A: 

my guess is that you are expecting JSON but you are getting a string.

mkoryak
+1  A: 

You either need to specify a dataType in the request like so:

$.ajax({
 type: "POST",
 url: "myURL.php",
 data: {username: username, password: password},
 dataType: "json", 
 success: function(results) {
  //THIS IS WHERE THE PROBLEM IS
  alert('Hi '+results.name); //Should be "Hi Basil Fawlty"
  }
});

or you can set the content type from php using

header( "Content-Type: application/json" );
Geoff
+3  A: 

If you are using jQuery < 1.4, you must specify dataType: "json".

As of 1.4, dataType defaults to:

Intelligent Guess (xml, json, script, or html)

But this requires that the response header contains the string "json". So you'll want to send:

header('Content-type: application/json');

This newly added flexibility with dataType allows handlers to respond to multiple returned types.

If the problem continues, you'll want to alert the entire response alert(results); to see what's actually being returned.

Lot of similar answers here. Not sure who started it but whoever did no doubt invaded Poland.

webbiedave
Thanks for the answer. I'd up vote twice if I could for the Poland comment!
Matt
We were all composing at the same time I think.
Geoff