skyros
skyros

Reputation: 21

jQuery ajax forms with json response

Im having troubles trying to figure out why my JSON data.email response is returning null. Can anyone advise?


//javascript
$.ajax(
{
 type: 'POST',
 url: 'process.php',
 dataType: 'json',
 data: { email : "[email protected]" },
 success: function(data)
 {
  alert("result = "+data.email);
 }
});

//php (process.php)
if ($_POST['email']) 
 $return['email'] = $_POST['email'];
else
 $return['email'] = "no email specified";

echo json_encode($return);

Upvotes: 2

Views: 1020

Answers (1)

jdc0589
jdc0589

Reputation: 7018

whoops, missed the call to json_encode. Still, you need to set the Content-Type of the response to 'application/json' in the php

Upvotes: 1

Related Questions