我有一个GET端点,URI为/ user / user-id. ‘user-id’是这里的路径变量.
在GET请求时如何设置路径变量?
这是我试过的:
$http.get('/user/:id',{ params: {id:key} });
而不是替换路径变量,id被附加为查询参数.
即我的调试器将请求URL显示为“http:// localhost:8080 / user /:id?id = test”
我的预期解决的网址应该是“http://localhost:8080/user/test”
$http的params对象用于查询字符串,因此您传递给参数的键值对将作为查询字符串键和值输出.
$http.get('/user',{ params: { id: "test" } });
成为:http:// localhost:8080 / user?id = test
如果您需要http:// localhost:8080 / user / test,您可以:
>自己构建url,
$http.get(‘/ user /’id);
>或者,使用$资源(特别是$resource.get https://docs.angularjs.org/api/ngResource/service/ $资源).这有点清洁.