ajax - Get and pass Json file with PHP -
i have json file in server:
file.json :
{"max":"512", "min":"1", ...
i 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 console undefined. why?
a possible solution is:
$response = array('json' => json_decode($json));
is effective solution?
in php reading file.json string , string putting array, javascript cant parse proper json object. use json_decode function in 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.
Comments
Post a Comment