我正在尝试从AJAX调用中获取数据。我测试了一个字符串,但它返回空。我只是想在响应时将字符串打印到控制台。
JS:
makeRequest: function(sService, oData){
var request = $.post(
'../../Utils/utils/php/userQuery.php'
,{'sService' : sService, 'oData': oData}
,'json'
);
request.done(function(oResult){
// This is firing off, but is printing empty string
console.log(JSON.stringify(oResult));
var fail = errorHandler.check(oResult);
if(!fail){
// This is firing off, but is printing empty string
console.log(oResult);
} else{
console.log(fail);
}
});
request.fail(function(oResult){
console.log("There was an error in retrieving service data");
});
}
PHP:
class request{
private $sService;
function _construct($sService){
$_SESSION['user_Id'] = 100000;
$this->$sService = $sService;
switch($this->$sService){
case 'follower':
break;
case 'followee':
break;
case 'promoter':
break;
case 'promotee':
break;
case 'note':
// This should be hit and return "hit"
echo json_encode("hit");
break;
case 'createFollowee':
break;
case 'createPromotee':
break;
case 'createNote':
break;
default:
echo "ErrorCode: 4000";
break;
}
}
}
$request = new request($_POST['sService']);
如果我尝试$.parseJSON(oResult)
响应,我会在解析时出错,因为它是空的。我哪里错了?
解答:
上面的更新代码有效。答案提出了几个问题。
我希望这有助于其他人。
答案 0 :(得分:2)
json_encode
您的输出__construct
(不是_construct
)$sService = $_POST['sService'];
应为$this->sService = $_POST['sService'];
new request();
答案 1 :(得分:0)
这可能是您的类构造函数无法正常运行。声明构造函数的正确语法是function __construct()
(两个下划线前面)或function theClassName()
。检查the PHP manual。
http://codepad.viper-7.com/k3vclY - 没有/空输出_construct()
一个下划线
http://codepad.viper-7.com/vabqqL - 输出两个下划线的__construct()