2016-08-11 46 views
0

我使用的是json-server,我可以發佈並獲取。但我無法進行更新。Json服務器更新

在我的數據庫,我有這樣的數據:

{ 
    "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");  
       }); 

我感謝您的幫助。

+0

嘗試使用關鍵字「put」而不是「update」。 – Luxor001

回答

0

update不是有效的HTTP方法。

您似乎在努力尋求基於REST的API。 REST模式基於HTTP協議,因此您使用的HTTP方法與您希望實現的實體的CRUD操作之間存在固有的映射關係。

在HTTP您可用以下方法:

  • POST - 一般的REST模式下用於創作。
  • PUT - 用於在REST範例下進行更新。
  • DELETE - 用於刪除。
  • GET - 通常用於閱讀。

在你的情況下,你應該使用PUT,因爲你想進行更新。

0

您可以使用PUT方法代替'updade'方法,然後它會自動將其轉換爲post方法,並且您的數據將被更新。