空值添加到JSON文件

时间:2016-11-27 23:21:09

标签: php angularjs json

我刚刚开始使用Angular,我正在尝试使用PHP脚本更新我的JSON文件。脚本使用正确的键更新它,但值为null。我真的很感激一些帮助。

这是我的代码:

<?php
$object_data = array(
'capital' => $_POST['in1'],
'country' => $_POST['in2'],
'continent' => $_POST['in3'],
'image' => $_POST['in4']
);
$capitals = file_get_contents('capitals.json');
$capital_data = json_decode($capitals);

array_push($capital_data,$object_data);
$jsondata = json_encode($capital_data,JSON_PRETTY_PRINT);

file_put_contents('capitals.json',$jsondata);
?>

这是我的JSON文件:

[
{
    "capital": "Ottawa",
    "country": "Canada",
    "continent": "North America",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/2\/22\/Parliament-Ottawa.jpg"
},
{
    "capital": "Oslo",
    "country": "Norway",
    "continent": "Europe",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/b\/bd\/Downtown_Oslo_Norway_skyline.png\/800px-Downtown_Oslo_Norway_skyline.png"
},
{
    "capital": "Auckland",
    "country": "New Zealand",
    "continent": "Oceania",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/6\/6e\/Auckland_City_from_Mt_Victoria%2C_Devonport_-_Flickr_-_111_Emergency_%281%29.jpg"
}
]

最后,这是我在Angular中的帖子请求:

  $http.post('capitalData.php',                         
  {"capital":input1.val(),"country":input2.val(),
  "continent":input3.val(),"image":input4.val()}).success(function(){
  alert("Added!");
  });

编辑:更新了帖子请求以指定我正在发送的数据,但它仍然返回空值。

0 个答案:

没有答案