0

我想知道如何编写PUT在路径中发送参数的请求。例如,dev 将 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);

以上可能是错误的,因为我不知道该怎么做。我很感激这个答案。谢谢。

4

1 回答 1

1

你可以这样做:

this._$http.put(`api/products${customerId}/product/${productId}`, payload);

注意:我正在使用模板文字:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/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(…));
   });
});
于 2019-05-21T16:31:09.470 回答