-3

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;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
red_sensor
  • 13
  • 1
  • 8
  • 1
    In the jQuery you say you want to retrieve JSON from the server. You are not sending back JSON from the server. As far as I can tell, that's the main problem. – Matt Gibson Jan 04 '14 at 19:10

3 Answers3

3

Your error is:

Unexpected token D

Then going to your site’s URL, the output is simply:

Distance: </br>Time in minutes: </br>Time in seconds: </br>Average speed: </br>

The “unexpected token” which is D is referring to the first letter of your output which is the word Distance.

It is unclear to me what the goal of the PHP code is. Do you expect that when it connects to that PHP code it takes the data and adds it to the database?

Meaning your path is currently now:

JavaScript Ajax via $_POST -> PHP parsing of $_POST -> And that PHP script is echoing contents?

What do those echos have to do with the process? What happens when you just comment out the echos?

Let’s look at your 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;

What exactly is this about? So in the first ones you are assigning variables:

$route = $_POST['route'];
$timeInMinutes = $_POST['timeInMinutes'];
$timeInSeconds = $_POST['timeInSeconds'];
$averageSpeed = $_POST['averageSpeed'];
$distance = $_POST['distance'];

Okay, and then in the next one you are using json_encode on those variables:

$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);

Then what are these?

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;

And what are you now doing with those json_encode variables? What is the goal of your script. It’s simply a mess.

Perhaps your code in the PHP file that is your server code should simply be this:

$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);

EDIT Also, looking at your JavaScript code, you have a trailing comma in your JSON. And as explained on this question & answer thread that is not allowed in the JSON spec. So I would change this:

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,
},

To be this; note the comma after total_km_rounded_str, is removed to make it simply 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
},
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • Thanks, I know it, how can get rid of this error? – red_sensor Jan 04 '14 at 19:18
  • Your solution works, but it returns "null" in all variables, how can I solve it? – red_sensor Jan 04 '14 at 19:30
  • 1
    @red_sensor I have 100% no idea what your code is trying to do. You want to post variables via JavaScript to PHP, but then you say the variables are empty? Do you actually check to see of `trackCoords_str`, `final_time_m_str`, `final_time_s_rounded_str`, `aver_speed_km_h_rounded_str`, `total_km_rounded_str` have values? – Giacomo1968 Jan 04 '14 at 19:47
  • Yes, they have values, when I had errors previously, variables were in this errors, they exist 100%. If you have no idea how to solve it please tell how to send data in html? – red_sensor Jan 04 '14 at 19:50
  • @red_sensor I have edited my answer to explain the problem might be in the way you are coding your JSON. Past that I have helped all I can. Best of luck! – Giacomo1968 Jan 04 '14 at 19:52
  • I removed it but result is same :( – red_sensor Jan 04 '14 at 19:56
  • If you want I can provide all source code, but I it`s too huge for StckOverflow. – red_sensor Jan 04 '14 at 19:57
  • Here is my full js code http://jsfiddle.net/9nxwg/ and link to my app http://test.whirlware.biz/client/ if it makes sense. Hope for your help! – red_sensor Jan 04 '14 at 19:59
1

The client expects to get JSON, so you need to send back valid JSON … and only valid JSON.

What this means is that you should really only call json_encode a single time. Your code should probably be more like:

echo json_encode(array(
    "distance" => $distance,
    "time in minutes" => $timeInMinutes
    //etc.
));

Either that, or just use HTML instead of JSON and remove the dataType: json line on the client.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

Delete the comma here

distance: total_km_rounded_str
                              ^ HERE
Black Sheep
  • 6,604
  • 7
  • 30
  • 51
  • 1
    Good answer. Too bad the original poster barely understands what their issues are. Look at the comments on my answer. – Giacomo1968 Jan 04 '14 at 20:03