且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从php脚本返回JSON对象

更新时间:2023-12-04 14:41:46

在PHP文件中,将内容类型更改为 application / json

In your PHP file, change the content type to application/json.

JS

$.get('/process.php', function(data) {      
    console.log(data);
} );

PHP

<?php

    header( "Content-type: application/json" );

    $jsonAnswer = array('test' => 'true');
    echo json_encode($jsonAnswer);

然后你的控制台应该读 Object {test:true} 而不仅仅是JSON字符串。

Then your console should read Object {test: "true"} rather than just the JSON string.