Json服务器更新

时间:2016-08-11 11:37:25

标签: angularjs json http

我正在使用json-server,我可以发帖并获取。但是我无法进行更新。

在我的数据库中,我有这些数据:

{
  "userRecipes": [
    {
      "id": 1,
      "desc": "dog",
      "flag" : true
    }
  ]
}

我希望更新标志,为此,我使用了此代码,但它不起作用:

loginDataSend.flag = true;     
$http
       (
          {
              method: 'update',
 url: 'http://localhost:3000/userRecipes/' + id,
   data: loginDataSend,
   dataType: "json"
    }
   ).error(funcion()
                 {
                 // Error code here
                 alert("error");
                 })
                 .success(function ()
                 {
                 alert("ok");    
                 });

我感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

update不是有效的HTTP方法。

您似乎正在努力使用基于REST的API。 REST范例建立在HTTP协议之上,因此您使用的HTTP方法与您希望实现的实体之间的CRUD操作之间存在固有的映射。

在HTTP中,您可以使用以下方法:

  • POST - 通常用于在REST范例下创建。
  • PUT - 用于REST范例下的更新。
  • DELETE - 用于删除。
  • GET - 通常用于阅读。

在您的情况下,您应该使用PUT,因为您想要进行更新。

答案 1 :(得分:0)

您可以使用PUT方法代替' updade'方法,然后它会自动将其转换为post方法,您的数据将会更新。