-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;
1
  • 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. Commented Jan 4, 2014 at 19:10

3 Answers 3

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
},
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I know it, how can get rid of this error?
Your solution works, but it returns "null" in all variables, how can I solve it?
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?
I removed it but result is same :(
If you want I can provide all source code, but I it`s too huge for StckOverflow.
|
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.

2 Comments

It works like in your example but on the server all variables return null, why?
I alos tried to change datatype to html but error was the same
1

Delete the comma here

distance: total_km_rounded_str
                              ^ HERE

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.