我有一个错误:
意外的令牌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;
答案 0 :(得分:3)
您的错误是:
意外的令牌D
然后转到your site’s URL,输出就是:
Distance: </br>Time in minutes: </br>Time in seconds: </br>Average speed: </br>
D
的“意外令牌”是指输出的第一个字母,即距离。
我不清楚PHP代码的目标是什么。您是否期望当它连接到该PHP代码时,它会获取数据并将其添加到数据库中?
现在意味着您的路径:
JavaScript Ajax通过$ _POST - &gt; PHP解析$ _POST - &gt;那个PHP 脚本是在回应内容吗?
echo
与此过程有什么关系?当你只注释掉echo
s?
让我们来看看你的代码:
$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;
这到底是什么意思?所以在第一个中你要分配变量:
$route = $_POST['route'];
$timeInMinutes = $_POST['timeInMinutes'];
$timeInSeconds = $_POST['timeInSeconds'];
$averageSpeed = $_POST['averageSpeed'];
$distance = $_POST['distance'];
好的,然后在下一个变量中使用json_encode
:
$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;
你现在用那些json_encode
变量做什么?你的脚本的目标是什么?这简直就是一团糟。
也许您的PHP文件中的代码就是您的服务器代码:
$json_output = array();
$json_output['route'] = $_POST['route'];
$json_output['timeInMinutes'] = $_POST['timeInMinutes'];
$json_output['timeInSeconds'] = $_POST['timeInSeconds'];
$json_output['averageSpeed'] = $_POST['averageSpeed'];
$json_output['distance'] = $_POST['distance'];
echo json_encode($json_output);
编辑此外,查看您的JavaScript代码,您的JSON中有一个尾随逗号。并且JSON规范中不允许使用as explained on this question & answer thread。所以我会改变这个:
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,
},
成为这样;请注意删除total_km_rounded_str,
后的逗号,使其仅为total_km_rounded_str
:
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
},
答案 1 :(得分:1)
客户端希望获得JSON,因此您需要发回有效的JSON ...和仅有效的JSON。
这意味着你真的应该只打一次json_encode
。你的代码应该更像是:
echo json_encode(array(
"distance" => $distance,
"time in minutes" => $timeInMinutes
//etc.
));
或者只是使用HTML而不是JSON并删除客户端上的dataType: json
行。
答案 2 :(得分:1)
删除此处的逗号
distance: total_km_rounded_str
^ HERE