开发者

jQuery - Access PHP array values after AJAX POST

I'd like to access a PHP array using JavaScript after a successful POST.

PHP Code:

return array('success' => true);

Javascript Code

$('#Get-Info').submit(function() {
$.post("info.php",
    function(data){
        if ( data['success'] ) {
            // Do things.
        }
    }
);
return false; });

The ja开发者_运维知识库vascript function is definitely running, it just can't access the PHP array.


Make the php return json. Not sure about this part as I'm not a php programmer, but the javascript would look like this:

$('#Get-Info').submit(function() {
$.post("info.php",
    function(data){
        if ( data['success'] ) {
            // Do things.
        }
    }, "json"
);
return false; });

The only difference being that jQuery will automatically parse the data as json, the datatype parameter. More info.

If I'm not horribly wrong, this should work for the php, although it requires PHP 5.2.0:

echo json_encode(array('success' => true));

More info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜