我在尝试使用 Restangular 发出发布请求时遇到问题:
我将像这样设置查询:
var auth = Restangular.all('auth');
var check = auth.one('check');
然后我将像这样执行发布请求:
var user = {
email: 'randomemail@gmail.com',
pass: 'randompass'
}
check.post(user)
但是,请求显示错误,当我检查网络时,请求是这样发送的:
http://localhost/auth/check/[object object]
为什么post请求像查询参数一样附加对象而不是在请求正文中发送它?
如果我错误地格式化了这个帖子请求,有人可以指出使用 Restangular 中的 one and all 来格式化帖子请求的正确方法吗?
谢谢!
最佳答案
当您发布到 one()
时,post()
实际上期望 subElement 作为第一个参数,这就是它附加传递到路径的对象的原因...
(来自 documentation )
post(subElement, elementToPost, [queryParams, headers]): Does a POST and creates a subElement. Subelement is mandatory and is the nested resource. Element to post is the object to post to the server
要发布到 /auth/check
,您可以使用 customPOST()
...
auth.customPOST(user, 'check');
编辑 - 如果您打算使用 post()
...,这里有几个示例...
Restangular.one('auth').post('check', user);
或者
auth.all('check').post(user);
关于javascript - 在 Restangular post 请求中使用单一和全部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24469922/