将JSON写入文件

I wont create a JSON file and save this with a php script. I got this error message (Firefox)

NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED: JavaScript component does not have a method named: "available"'JavaScript component does not have a method named: "available"' when calling method: [nsIInputStream::available]

When i use Chrome I didn't get the error message but it doesn't work too.

JavaScript:

var jsonString = '{ "foo": 1, "bar": { "baz" : 1 } }';
var data = JSON.parse( jsonString );
var xhr = new XMLHttpRequest();     
xhr.open('POST', './php/upload.php', true);
xhr.send(data); 

PHP:

$dateiname = "test12345.json"; 

if (isset($_POST) && count($_GET)==0) { 

    $jsonObj = $_POST; 
    $fh = fopen($dateiname, 'w'); 
    $input = json_encode($jsonObj); 
    fwrite($fh, $input); 
    fclose($fh); 
}

The Javascript should use:

xhr.send(jsonString);

because the data being sent must be a string, not an object.

In PHP, to read the raw post data, you should use:

$rawjson = file_get_contents("php://stdin");
$jsonObj = json_decode($rawjson);

$_POST can only be used for form data that's formatted using URL-encoding or multipart/form-data encoding.