我有一个JSON对象,如下所示:
{"name":"name","description":"description","picture1":"link","picture2":"link","picture3":"link"}
发送到这样的php文件:
var request = new XMLHttpRequest();
var phpfile = "http://mayar.abertay.ac.uk/~1605524/coursework/createVenue.php";
request.open("POST", phpfile, !0);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.onreadystatechange = function(){
if(this.readyState == 4) {
if(this.status == 200) {
window.alert("Success");
}
}
}
request.send(json);
我尝试在PHP中获取JSON对象的内容,如下所示:
header('Content-type: application/json');
$json = file_get_contents('php://input');
$json_decode = json_decode($json ,true);
$venuename = $json_decode -> name;
$description = $json_decode -> description;
$pictureone = $json_decode -> picture1;
$picturetwo = $json_decode -> picture2;
$picturethree = $json_decode -> picture3;
数据应该写在数据库中,然后它在我通过url传递数据作为参数之前工作,但是现在它创建了一个带有空字段的数据库条目。我认为我的错误是在PHP中访问JSON对象,但我无法找到解决方案。
解
数据访问必须这样做:
$venuename = $json_decode[name];