jQuery ajax与json响应形成

时间:2011-07-06 01:54:56

标签: php jquery ajax json

我在试图弄清楚为什么我的JSON data.email响应返回null时遇到了麻烦。有人可以建议吗?


//javascript
$.ajax(
{
 type: 'POST',
 url: 'process.php',
 dataType: 'json',
 data: { email : "me@home.com" },
 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);

1 个答案:

答案 0 :(得分:1)

哎呀,错过了对json_encode的调用。 不过,您需要在php

中将响应的Content-Type设置为'application / json'