我正在使用jQuery的ajax方法将一些数据发布到服务器并取回响应。虽然服务器端php代码正在返回一个json编码的字符串/数组,但响应会返回为空。PHP函数不返回任何数据给JQuery ajax方法
有人能指出我犯的错误吗?下面是如何使用我正在触碰postData.php页面的jquery ajax方法。
$.ajax({
url:'postData.php',
type:'POST',
data:data,
dataType: "json",
success: function(response){
console.log(response);
}
});
postData.php中的内容非常直接,因为我仍在开发它。
$data = array();
//inside postData.php
$data['test']=1;
return json_encode($data);
它应该返回一个json字符串,但它返回null。我也尝试在$ data数组声明后面回显一个字符串,它在firebug中回显它,但是响应是当我在成功回调中执行console.log时,它会返回为空。
谢谢!!我已经使用jquery和ajax了很多,并且急于快速完成任务,我犯了这个有趣的错误! – macha 2011-05-23 01:00:29