[英]Get JSON from Javascript to PHP
I have a javascript function that sends JSON to my server: 我有一个javascript函数将JSON发送到我的服务器:
$("#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/",
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"
});
});
And mix of PHP and JS code that receive and display my JSON data 混合接收和显示我的JSON数据的PHP和JS代码
<?php
$route = $_POST['route'];
$timeInMinutes=$_POST['timeInMinutes'];
$timeInSeconds=$_POST['timeInSeconds'];
$averageSpeed=$_POST['averageSpeed'];
$distance=$_POST['distance'];
$trackCoords = json_decode($route, false);
$total_km_rounded = json_decode($timeInMinutes, false);
$final_time_m = json_decode($timeInSeconds, false);
$final_time_s_rounded = json_decode($averageSpeed, false);
$aver_speed_km_h_rounded = json_decode($distance, false);
echo $trackCoords['coordsarray'];
echo $total_km_rounded;
echo $final_time_m;
echo $final_time_s_rounded;
echo $aver_speed_km_h_rounded;
?>
<script type="text/javascript">
var total_km_rounded = '<?php echo $total_km_rounded ?>';
document.write('Растояние: ' + total_km_rounded);
var final_time_m = '<?php echo $final_time_m ?>';
document.write('Растояние: ' + final_time_m);
var final_time_s_rounded = '<?php echo $final_time_s_rounded ?>';
document.write('Растояние: ' + final_time_s_rounded);
var aver_speed_km_h_rounded = '<?php echo $aver_speed_km_h_rounded ?>';
document.write('Растояние: ' + aver_speed_km_h_rounded);
</script>
But when I send JSON data my server don`t display it. 但是当我发送JSON数据时,我的服务器不会显示它。 Where did I make a mistake? 我在哪里弄错了? Maybe I can receive JSON another way? 也许我可以用另一种方式接收JSON?
Try this, success: function(response) {alert('Success!');},
after the data
, not inside the data
试试这个, success: function(response) {alert('Success!');},
在data
,不在data
内部
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
},
success: function(response) {alert('Success!');},
You should JSON.stringify an array like this( general convention ): 你应该像这样JSON.stringify一个数组( 一般约定 ):
//rough code
data['trackCoords_str'] = trackCoords;
data['final_time_m_str'] =final_time_m;
data['final_time_s_rounded_str'] = final_time_s_rounded;
data['aver_speed_km_h_rounded_str'] = aver_speed_km_h_rounded;
data['total_km_rounded_str'] = total_km_rounded;
$.ajax({
url: "http://test.whirlware.biz/server/",
type: "POST",
data: JSON.stringify(data),
dataType: "json",
success: function(){ alert('success!'); },
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
alert(thrownError);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.