您正在双重编码。无需使用JS 和 PHP 进行编码,只需在一侧进行编码, 然后 再进行一次编码。
// step 1: build data structurevar data = { metros: graph.getVerticies(), routes: graph.getEdges()}// step 2: convert data structure to JSON$.ajax({ type : "POST", url : "json.php", data: { json : JSON.stringify(data) }});请注意,该
dataType参数表示预期的 响应 类型,而不是您将数据发送为的类型。
application/x-www-form-urlenpred默认情况下,发帖请求将被发送。
我认为您根本不需要该参数。您可以将其缩减为:
$.post("json.php", {json : JSON.stringify(data)});然后(在PHP中)执行以下操作:
<?php $json = $_POST['json']; if (json_depre($json) != null) { $file = fopen('new_map_data.json','w+'); fwrite($file, $json); fclose($file); } else { // user has posted invalid JSON, handle the error }?>


