2016-03-03 78 views
0

我的朋友已經從另一個帳戶發佈,但沒有人回覆。我希望這次有人能幫助我。自從很多天以來,我們都陷入非常嚴重的困境做了很多排除故障但徒勞無功!
我們正在努力更新ID的基礎上的數據,但它提供了以下錯誤:PUT錯誤400使用角度js和REST Web服務的錯誤請求

PUT http://localhost:17681/api/perItemDetails/2 400 (Bad Request) 

的鏈接工作完美,但無法更新表中的數據。 下面是我對角的js更新數據代碼:

$scope.updateSave=function() 
    { 
     var updateid=this.meraId; 
     var bb2price=this.bbprice; 
     var maxbbPrice=this.mpbbprice; 
     var vipPrice=this.vip_price; 
     var retailPrice=this.retailprice; 
     var sname=this.sname; 
     var ptype=this.ptype; 
     var useragent=this.userAgent; 
     var obj = { 
      bbPrice: bb2price, 
      maxbbPrice: maxbbPrice, 
      vipPrice: vipPrice, 
      retailPrice:retailPrice, 
      userNames:useragent, 
      pType:ptype, 
      sName:sname 
     }; 

     $http({ 
      method: 'put', 
      url: "http://localhost:17681/api/perItemDetails/" + updateid, 
      data: JSON.stringify(obj), 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }). 
     success(function (data, status, headers, config) { 
      alert("updated succesfully"); 
     }). 
     error(function (data, status, headers, config) { 
      console.log('Error: ' + status); 
     }); 
    } 

以下是我更新網頁API代碼:

// PUT: api/perItemDetails 
     [ResponseType(typeof(void))] 
     public IHttpActionResult PutperItemDetail(int id, perItemDetail perItemDetail) 
     { 
      if (!ModelState.IsValid) 
      { 
       return BadRequest(ModelState); 
      } 

      if (id != perItemDetail.id) 
      { 
       return BadRequest(); 
      } 

      db.Entry(perItemDetail).State = EntityState.Modified; 

      try 
      { 
       db.SaveChanges(); 
      } 
      catch (DbUpdateConcurrencyException) 
      { 
       if (!perItemDetailExists(id)) 
       { 
        return NotFound(); 
       } 
       else 
       { 
        throw; 
       } 
      } 

      return StatusCode(HttpStatusCode.NoContent); 
     } 

請不要將其標記爲重複或不合適的,而不是幫助我們。
在此先感謝:)

回答

0

它似乎沒有將您的id添加到您要發送的對象。

var obj = { 
      bbPrice: bb2price, 
      maxbbPrice: maxbbPrice, 
      vipPrice: vipPrice, 
      retailPrice:retailPrice, 
      userNames:useragent, 
      pType:ptype, 
      sName:sname 
     }; 

這意味着

if (id != perItemDetail.id) 
      { 
       return BadRequest(); 
      } 

將返回錯誤的請求。

試加

var obj = { 
      id: updateId, 
      bbPrice: bb2price, 
      maxbbPrice: maxbbPrice, 
      vipPrice: vipPrice, 
      retailPrice:retailPrice, 
      userNames:useragent, 
      pType:ptype, 
      sName:sname 
     }; 

你也可以嘗試刪除JSON字符串化,因爲它不應該是必要的。

data: JSON.stringify(obj), 

data : obj 
+0

首先感謝您的回覆@Ashley先生的,但通過自己的方式不工作也是如此。嘗試時,它會在Web服務代碼中的db.savechanges()行中發出一般異常。 –

+0

這意味着服務器不再響應400錯誤? –

相關問題