JSON和PHP的Parsererror

This is my first time handling with JSON, and I can't make this work:

JavaScript:

var obj = new Object();
obj.latitude = sessionStorage.lat;
obj.longitude  = sessionStorage.lng;
var jsonString= JSON.stringify(obj);
var post_data = "coords="+jsonString;

$.ajax({
url: "index.php",
type: "POST",
data: post_data,
dataType: "json",
success: function(){
    alert("postdone!");
},
error: function(){
    alert("posterror!")
}
});

PHP:

if (isset($_POST['data'])) {
    $jsondata = json_decode($_POST['data'],true);
    echo $jsondata;
}

I always get "posterror!"...

you should use this code for send data in php

type: "POST",
data: {data:post_data},

http://php.net/manual/en/function.json-decode.php

and into json in PHP you can decode json for PHP

You have some things done

data: Object must be Key/Value pairs. http://api.jquery.com/jquery.ajax/

JS

var obj = new Object();
obj.latitude = sessionStorage.lat;
obj.longitude  = sessionStorage.lng;
var jsonString= JSON.stringify(obj);
//does not work, you had to get rid of the string in before dejsoning it in php
var post_data = /*"coords="+*/jsonString;

$.ajax({
 url: "index.php",
 type: "POST",
 //watch it here
 data: {"data": post_data},
 //does not work if you do not send json back
 dataType: /*"json"*/ "html",
 success: function(){
     alert("postdone!");
 },
 error: function(){
     alert("posterror!")
 }
});

PHP

Should work now

if (isset($_POST['data'])) {
    $jsondata = json_decode($_POST['data'],true);
    //Where should that be good for?
    echo $jsondata;
}

USe This

var obj = new Object();
obj.latitude = sessionStorage.lat;
obj.longitude  = sessionStorage.lng;
var post_data = JSON.stringify(obj);

$.ajax({
url: "index.php",
type: "POST",
data: post_data,
dataType: "json",
success: function(){
    alert("postdone!");
},
error: function(){
    alert("posterror!")
}
});

<?php 
// php file

  if (isset($_POST['data'])) {
    $jsondata = json_decode($_POST['data'],true);
    echo $jsondata;
}

?>

The code could be simplified a bit

var post_data = {
    coords: {
        latitude: sessionStorage.lat,
        longitude: sessionStorage.lng
    }
}


$.ajax({
    url: "index.php",
    data: JSON.stringify({data: post_data}),
    contentType: 'application/json',
    success: function(){
        alert("postdone!");
    },
    error: function(){
        alert("posterror!")
    }
});

As for php:

if (isset($_POST['data'])) {
    $jsondata = json_decode($_POST['data'], true);
    echo $jsondata;
}