我想知道如何编写路径中发送参数的PUT
请求。例如,开发人员更改了使用查询字符串作为参数使用路径中的参数的put请求的URL。当访问发送作为查询时,我做了这样的事情:
let payload = {
product_id: this.productId,
customer_id: this.customerId,
userGuide_id: this.userGuide
}
return this._$q((resolve, reject) => {
this._$http.put(‘/api/products/customer/mostRecent’, payload)
.then((result) => resolve(result))
.catch((err) => {
reject(…));
});
});
简单。
但是,现在更改了PUT请求以在路径中使用参数,即:
PUT api/products/customer/{customerId}/product/{productId}
我到底怎么写?
let customer_id = this.customerId,
product_id = this.productId;
let payload = {
user_GuideId: this.userGuideId
}
this._$http.put(“api/products/”+customer_id+“/product/”+product_id, payload);
以上可能是错误的,因为我不知道该怎么做。感谢答案。谢谢。
您可以做这样的事情:
this._$http.put(`api/products${customerId}/product/${productId}`, payload);
注意:我正在使用模板文字:https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/template_literals
谢谢!
更新:
let payload = {
product_id: this.productId,
customer_id: this.customerId,
userGuide_id: this.userGuide
}
return this._$q((resolve, reject) => {
this._$http.put(`api/products${payload.customer_id}/product/${payload.product_id}`, payload);
.then((result) => resolve(result))
.catch((err) => {
reject(…));
});
});