[英]Unexpected behaviour while decoding strings in PHP (from AJAX POST call)
[英]decoding the response from $.post
这是我的JavaScript
<script>
function checkEmail(email) {
$.post('<?=base_url()?>checkemail',{ emailid : $('#inputEmailId').val() },function(d){
CC = eval(d);
alert(CC);
});
}
</script>
PHP文件包含的一部分
if ($parts) { //these details exist
$obj->status = 'yes';
}
else {
$obj->status = 'uni';
}
$res[] = $obj;
当我print_r($res)
我得到status=>'yes'
但是当我在我的JavaScript中alert(CC)
,我得到了
function array() {
[native code]
}
如何提醒JavaScript status
?
如评论中所述, php
部分应包括对json_encode
的调用
if ($parts) { //these details exist
$obj->status = 'yes';
}
else {
$obj->status = 'uni';
}
// sending out $res should do it
// you can also add proper JSON headers if the content is not properly formatted
header('Content-Type: application/json');
print json_encode($obj);
您的JavaScript应该看起来像这样
function checkEmail(email) {
$.post('<?=base_url()?>checkemail',{ emailid : $('#inputEmailId').val() },function(d){
alert(d.status);
});
}
php部分可以像这样更改。
而不是使用object
和array
而是使用variable
。 无需为了简单的事情而复杂地使用对象和数组。
if ($parts) { //these details exist
$status = 'yes';
}
else {
$status = 'uni';
}
echo $status;
这工作的PHP
if ($parts) { //these details exist
$obj->status = 'yes';
}
else {
$obj->status = 'uni';
}
$res[] = $obj;
$data['content'] = json_encode($res);
Java脚本
$.post('<?=base_url()?>checkemail',{ emailid : $('#inputEmailId').val() },function(d){
CC = eval(d);
alert(CC[0].status);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.