向JSON添加行不能在其他服务器上运行

时间:2016-05-11 11:14:03

标签: php json

我的PHP和JSON有一个小问题。所有的东西都可以在我的测试服务器上运行,但不能在我的真实服务器上运行。

问题是我的POST不会保存在我的comment.json ...

这是我的AJAX

function sendContentToFile(id, name){   

    var idB, nameB, d, tag, stunden, minuten, month, monat,datum, content;
    d = new Date();
    tag = d.getDate();
    stunden = d.getHours();
    minuten = d.getMinutes();
    month = new Array();
    month[0] = "Januar";
    month[1] = "Februar";
    month[2] = "März";
    month[3] = "April";
    month[4] = "Mai";
    month[5] = "Juni";
    month[6] = "Juli";
    month[7] = "August";
    month[8] = "September";
    month[9] = "Oktober";
    month[10] = "November";
    month[11] = "Dezember";
    monat = month[d.getMonth()];
    if(minuten <= 9)  {
        minuten = 0+ d.getMinutes();
    } 
    datum = tag + ". " + monat + " , "+ stunden + ":" + minuten;
    idB = id;
    nameB = name;
    content = document.getElementById("textarea"+nameB+'0'+idB).value;
    console.log(content);
var url = "comments.php";

var neuesObject = {"Name" : nameB, "id" : idB, "comment" : content,"datum" : datum  };
var http = new XMLHttpRequest();

http.open('POST', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function() {//Call a function when the state changes.
           if(http.readyState == 4 && http.status == 200) {

                window.location.reload();
                }
            };
if (content!= "") {             
http.send('id='+idB+'&name='+nameB+'&comment='+content+'&datum='+datum);
 } else {
alert('Einfügen');
}
}

我的PHP文件

header("Content-Type: application/json");
$name = $_POST["name"];
$id = $_POST["id"];
$comment = $_POST["comment"];
$datum = $_POST["datum"];
$AdditionalArray = array(
            'id' => $id,
            'name' => $name,
            'comment' => $comment,
            'datum' => $datum
            );



            //open or read json data
            $data_results = file_get_contents('comments.json');

            $tempArray = json_decode($data_results);
            //append additional json to json file
            $tempArray[]=$AdditionalArray;
            $jsonData = json_encode($tempArray);

        file_put_contents('comments.json', $jsonData);

有人知道这种东西有什么问题吗?

0 个答案:

没有答案