2017-09-10 42 views
0

同樣失敗這是JSON字符串我傳遞到POST請求:JSON字符串獲取郵差正確解析,但在JavaScript

var data = { 
      "location": { 
      "value": { 
       "href": "http://localhost:8080/restful/objects/businessentities.Location/47" 
      } 
      }, 
      "asset": { 
      "value": { 
       "href": "http://localhost:8080/restful/objects/businessentities.Asset/36" 
      } 
      } 
    } 

的AngularJS資源代碼:

var AuditItemCreate = $resource(
    apiUrl + '/restful/services/domainapp.dom.BusinessEntities.AuditItemMenu/actions/create/invoke/'); 

var a2 = new AuditItemCreate(); 
a2.$save(data) 
    .then(function(res) { console.log("success") }) 
    .catch(function(req) { console.log("error saving obj: " + JSON.stringify(req, null, 4)); }) 

然而我得到一個服務器響應422(不可處理的實體)錯誤:

error saving obj: { 
    "data": { 
     "asset": { 
      "value": "{\"value\":{\"href\":\"http://localhost:8080/restful/objects/businessentities.Asset/36\"}}", 
      "invalidReason": "Expected a link (because this object's type is not a value) but found no 'href'" 
     }, 
     "location": { 
      "value": "{\"value\":{\"href\":\"http://localhost:8080/restful/objects/businessentities.Location/47\"}}", 
      "invalidReason": "Expected a link (because this object's type is not a value) but found no 'href'" 
     }, 
     "x-ro-invalidReason": "Mandatory" 
    }, 

相同的JSON字符串工作s在Postman中使用時會成功。

回答

0

我能夠解決這個問題 - 通過使用自己的資源類,而不是它的資源實例。