У меня есть ошибка:ошибка PHP + JavaScript: Неожиданный маркер D
Unexpected token D
Когда я пытаюсь отправить JSON из JavaScript в PHP. Я знаю, что ошибка вызвана на стороне сервера, и я попытался ее исправить, но затем я получаю еще одну ошибку, например Unexpected input
, Unexpected end
и тому подобное.
Я пытаюсь исправить это в течение 1 недели и не имеет результата. Моя цель - отправить данные на сервер и сделать сервер без ошибок, как я могу это сделать?
Мой код клиента:
$("#sendRoute").live('click', function() {
trackCoords_str = JSON.stringify(trackCoords);
final_time_m_str = JSON.stringify(final_time_m);
final_time_s_rounded_str = JSON.stringify(final_time_s_rounded);
aver_speed_km_h_rounded_str = JSON.stringify(aver_speed_km_h_rounded);
total_km_rounded_str = JSON.stringify(total_km_rounded);
$.ajax({
url: "http://test.whirlware.biz/server/index.php",
type: "POST",
data: {
route: trackCoords_str,
timeInMinutes: final_time_m_str,
timeInSeconds: final_time_s_rounded_str,
averageSpeed: aver_speed_km_h_rounded_str,
distance: total_km_rounded_str,
},
dataType: "json",
success: function(){alert("success!");},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
alert(thrownError);
}
});
});
Мой код сервера:
$route = $_POST['route'];
$timeInMinutes = $_POST['timeInMinutes'];
$timeInSeconds = $_POST['timeInSeconds'];
$averageSpeed = $_POST['averageSpeed'];
$distance = $_POST['distance'];
$trackCoords1 = json_encode($route);
$final_time_m1 = json_encode($timeInMinutes);
$final_time_s_rounded1 = json_encode($timeInSeconds);
$aver_speed_km_h_rounded1 = json_encode($averageSpeed);
$total_km_rounded1 = json_encode($distance);
echo "Distance: </br>"; echo $distance;
echo "Time in minutes: </br>"; echo $timeInMinutes;
echo "Time in seconds: </br>"; echo $timeInSeconds;
echo "Average speed: </br>"; echo $averageSpeed;
В JQuery вы говорите, вы хотите получить JSON от сервера. Вы не отправляете JSON с сервера. Насколько я могу судить, это главная проблема. –