解码来自 $.post 的响应

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'

但是当我 alert(CC) 在我的 javascript 我得到

function array() {
[native code]
}

如何提醒 javascript 中的 status

php部分可以这样改。

不使用 objectarray,而是使用 variable。无需为了简单的事情而将对象和数组复杂化。

if ($parts) { //these details exist
    $status = 'yes';
    }
else {
    $status = 'uni';
}

echo $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

if ($parts) { //these details exist
        $obj->status = 'yes';
        }
    else {
        $obj->status = 'uni';
    }

    $res[] = $obj;

    $data['content'] = json_encode($res);

Javascript

$.post('<?=base_url()?>checkemail',{ emailid : $('#inputEmailId').val() },function(d){
                CC = eval(d);
                alert(CC[0].status);
            });