使用PHP获取并传递Json文件

I have a Json file in the server:

file.json :

{"max":"512", "min":"1", ...

I get it with an ajax call:

$.ajax({                                      
    url: 'load_json.php',
    type: "POST",
    data: { id: id },
    dataType: 'json',
    success: function(resp) {
        console.log(resp.json.max);
    }
});

Where load_json.php is:

    $json = file_get_contents("file.json");
    $response = array('json' => $json);
    echo json_encode($response);

But in the console I get undefined. Why?

A possible solution is:

$response = array('json' => json_decode($json));

Is this the most effective solution?

In your php you are reading file.json as a string and then this string is putting to the array, so javascript cant parse it as a proper json object. Use json_decode function in your php code:

$json = file_get_contents("file.json");
$response = array('json' => json_decode($json)); //here
echo json_encode($response);

or:

$json = file_get_contents("file.json");
$response = '{"json":' . $json . '}'; //here
echo $response;

but use first solution.