【问题标题】:Json server updateJson 服务器更新
【发布时间】:2016-08-11 11:37:25
【问题描述】:

我正在使用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");    
                 });

感谢您的帮助。

【问题讨论】:

  • 尝试使用关键字“put”而不是“update”。

标签: angularjs json http


【解决方案1】:

update 不是有效的 HTTP 方法。

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

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

  • POST- 一般用于REST范式下的创建。
  • PUT - 用于 REST 范式下的更新。
  • DELETE - 用于删除。
  • GET - 一般用于阅读。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 2021-07-11
      • 1970-01-01
      • 2021-11-05
      • 2020-01-18
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      相关资源
      最近更新 更多