我有一個 PHP 檔案,我在其中從 geoJSON 檔案獲取資料,我正在瀏覽資料并將我需要的內容存盤在一個新的關聯陣列中,然后對其進行排序。我需要對此 PHP 檔案進行 AJAX 呼叫以獲取排序后的資料,但它拋出“SyntaxError: Unexpected token a in JSON at position 0”,“a”是“a”array ...來自回應,所以我相信它會作為一個字串通過。
我的 PHP 檔案:
<?php
$countryBordersJson = file_get_contents("../js/countryBorders.geojson");
$countryBordersJsonData = json_decode($countryBordersJson, true);
$dataLength = count($countryBordersJsonData['features']);
$countryNames = array();
for($i = 0; $i < $dataLength; $i ) {
$countryName = $countryBordersJsonData['features'][$i]['properties']['name'];
$countryIsoa2 = $countryBordersJsonData['features'][$i]['properties']['iso_a2'];
$country[$i]['countryName'] = $countryName;
$country[$i]['iso_a2'] = $countryIsoa2;
array_push($countryNames, $country[$i]);
}
sort($countryNames);
$data = json_encode($countryNames);
$decode = json_decode($data, true);
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['data'] = $decode;
header('Content-Type: application/json');
var_dump($output);
?>
我的 AJAX 請求:
$.ajax({
url: "libs/php/countryBorders.php",
dataType: "json",
type: "GET",
success: function(result) {
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.warn(jqXHR.responseText, textStatus, errorThrown);
}
})
我仍然在掌握 JSON、PHP 等,因此將不勝感激。
uj5u.com熱心網友回復:
您是否使用 var_dump 發送資料?它不能那樣作業。您需要發送一個字串:
echo json_encode($output);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/472071.html
