[英]Angular2 how to correct put request
Ok then, I have a single problem, the 好吧,我有一个问题,
Request URL: http://.../rest/1.0/brand/test145 Request Method:PUT Status Code:500 Internal Server Error Remote Address:... 请求网址: http://.../rest/1.0/brand/test145请求方法:PUT状态码:500内部服务器错误远程地址:...
Request payload is ok { "name": "test145" } 请求有效负载正常{“ name”:“ test145”}
addBrand(name : string){
let body = JSON.stringify(name)
let url = this.baseUrl + '/brand/' + name;
return this.http.put(url, { name } ).
map(res => res.json());
}
addBrand(name:any){
if(!name){return;}
this.BrandService.addBrand(name)
.subscribe(
name => this.name.push(name),
error => this.errorMesage = <any>error);
}
But the put should look like http://.../rest/1.0/brand/ and then, should be only request payload. 但是put应该看起来像http://.../rest/1.0/brand/ ,然后只能是请求有效负载。 When I remove that "name" from, it doesn't load the payload.
当我从中删除该“名称”时,它不会加载有效负载。
stricly PUT is update, so the url should be in format: 严格来说,PUT是更新的,因此该网址应采用以下格式:
http://.../rest/1.0/brand/test145 where the last part is unique id (you need to indicate what you mean to update) http://.../rest/1.0/brand/test145 ,其中最后一部分是唯一ID(您需要指出要更新的内容)
If you expect to create a record, you should rather POST to http://.../rest/1.0/brand 如果您希望创建记录,则应该将其发布到http://.../rest/1.0/brand
But you can always choose to deviate from conventions. 但是,您始终可以选择偏离约定。 In order to PUT to
.../brand
with a payload { "name": "test145" }
you would need to: 为了使用有效负载
{ "name": "test145" }
将.../brand
{ "name": "test145" }
到.../brand
,您需要:
addBrand(name : string){
let body = JSON.stringify({ name: name })
let url = this.baseUrl + '/brand'
return this.http.put(url, body ).
map(res => res.json());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.