I have an error:
Unexpected token D
When I try to send JSON from JavaScript to PHP. I know that error is caused on server side and I tried to fix it, but then I get another errors like Unexpected input
, Unexpected end
and similar.
I am trying to fix it during 1 week and have no result. My goal is to send data to server and make server show it without errors, how can I do it?
My client code:
$("#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);
}
});
});
My server code:
$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;