Restangular post

时间:2013-12-20 00:38:29

标签: angularjs rest restangular

我正在尝试在我的项目中使用restangular,我有设置和配置所有工作正常。 我需要发布一个帖子请求,其中我有url和json有效负载。

var url = xyz.com + "/api/v2/create?type=project";
eg payload
       '{
        "class":"Project",
        "name":"test_project",
        "description":"test_project",
        "owner":{"class":"User","id":"2"},
        "connection":{"class":"Connection","id":"1"},
        "defaultNamespace":"default",
        "namespaces":["default"]}'

如果帖子的有效载荷为params,我不确定如何进行重新调用? 我在下面尝试了这个,但似乎没有用。

Restangular.all('create').post({
        "class":"Project",
        "name":"test_project",
        "description":"test_project",
        "owner":{"class":"User","id":"2"},
        "connection":{"class":"Connection","id":"1"},
        "defaultNamespace":"default",
        "namespaces":["default"]})

1 个答案:

答案 0 :(得分:3)

嘿,你应该这样做:

Restangular.all('create').post(
// First the object
{
  class: "Project",
  name: "Test-project",
  owner: {
    class: "User"
  }  
},
// Then the request params
{type: "Project"}
);

应该这样做:)