javascript Restangular POST 始终为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17302395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:48:13  来源:igfitidea点击:

Restangular POST always empty

javascriptrestangularjsrestangular

提问by Carlos Bergen

I think I'm not understanding how a POST is done in a RESTful api. When creating a new object in Restangular with this:

我想我不明白 POST 是如何在 RESTful api 中完成的。使用以下命令在 Restangular 中创建新对象时:

var user = {name: "John", id:"123"};
Restangular.one('building','5').post(user);

I expect it to pass a $_POST array with the values of user to the url example.com/api/building/5

我希望它将带有用户值的 $_POST 数组传递给 url example.com/api/building/5

And right know it's doing a POST request to the correct script but the $_POST array is empty. Any idea of what I'm doing wrong?

并且正确知道它正在对正确的脚本执行 POST 请求,但 $_POST 数组为空。知道我做错了什么吗?

回答by mgonto

I'm the creator of Restangular. Posts should be done to collections, not to elements. So, if you want to add a user to the building, you should do something like:

我是 Restangular 的创建者。应该对集合进行发布,而不是对元素进行发布。因此,如果您想将用户添加到建筑物中,您应该执行以下操作:

Restangular.one("building", 5).post('users', user).then(function(postedUser) {
    console.log("Success");
})

Check the post method here: https://github.com/mgonto/restangular#element-methods

在此处检查发布方法:https: //github.com/mgonto/restangular#element-methods

The signature is path to subelement collection, element to POST.

签名是子元素集合的路径,元素到 POST。

Bests!

最好的!