I have a javascript function that sends JSON to my server:
$("#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
<?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. Where did I make a mistake? Maybe I can receive JSON another way?
-
The first thing is, the success function. Look the documentation. The location are wrong.Rafael– Rafael2013年12月31日 15:48:04 +00:00Commented Dec 31, 2013 at 15:48
-
I know, thanks, mybe you know another reason that can cause problem?red_sensor– red_sensor2013年12月31日 17:25:18 +00:00Commented Dec 31, 2013 at 17:25
2 Answers 2
Try this, success: function(response) {alert('Success!');}, after the data, not inside the 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!');},
answered Dec 31, 2013 at 15:47
Krish R
22.7k7 gold badges54 silver badges60 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
red_sensor
I deleted success: function(response) {alert('Success!');}, it`s not a reason of problem
Krish R
I didn't say, this is the answer, this one also caused the issues.!
You should JSON.stringify an array like this(general convention):
//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);
});
});
12 Comments
red_sensor
I tried but it doesn`t work, I changed variables in index.php too but no result.
Momin
can you echo the posted data from php back to this javascript with success() function?
red_sensor
I added success: function(){ alert('success!'); }, error: function(){ alert('error!'); } And when I send JSON it shows "error" so my JSON is not sent. What can be the reason?
Momin
please see the error with this after success:: error: function (xhr, ajaxOptions, thrownError) { alert(xhr.responseText); alert(thrownError); }
red_sensor
I got 2 errors: clip2net.com/s/6vn7Ri and clip2net.com/s/6vn8oE But I don`t know what Syntax error here, I send only numbers data
|
default