简体   繁体   中英

Restangular POST always empty

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

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

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?

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.one("building", 5).post('users', user).then(function(postedUser) {
    console.log("Success");
})

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

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

Bests!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM