Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在http.put请求中使用路径参数_Javascript_Angularjs_Xmlhttprequest_Put_Path Parameter - Fatal编程技术网

Javascript 如何在http.put请求中使用路径参数

Javascript 如何在http.put请求中使用路径参数,javascript,angularjs,xmlhttprequest,put,path-parameter,Javascript,Angularjs,Xmlhttprequest,Put,Path Parameter,我想知道如何编写在路径中发送参数的PUT请求。例如,dev将PUT请求的URL从使用查询字符串作为参数更改为使用路径中的参数。当参数作为查询发送时,我做了如下操作: let payload = { product_id: this.productId, customer_id: this.customerId, userGuide_id: this.userGuide } return this._$q((resolve, reject) => { this._$

我想知道如何编写在路径中发送参数的
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(…));
   });
});
this._$http.put(`api/products${customerId}/product/${productId}`, payload);

简单

但是,现在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);

以上可能是错误的,因为我不知道如何做到这一点。我很感激你的回答。谢谢。

您可以这样做:

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(…));
   });
});
this._$http.put(`api/products${customerId}/product/${productId}`, payload);

注意:我使用的是模板文本:

谢谢大家!

更新:

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(…));
   });
});

以上是正常的方式…@PetrAveryanov所以我的代码是正确的?如果它工作-是的)我的意思是这里没有什么复杂的-你可以用任何你喜欢的方式组合url。如果您使用es6,您可以使用${}表示法,如下面的答案中所示。我将如何添加有效负载?因为还需要其他数据,但不是路径中的参数。@Chris22我更新了我的答案。谢谢你,如果你还有任何问题,请告诉我。我刚刚再次更新,请看一看——谢谢@嗨,随时欢迎你!“哦,有一个问题,您正在访问有效负载对象的参数,即使这些参数没有在该对象中定义?”我更新了我的答案,其中也包括有效负载对象。在这个有效负载对象中:`product\u id:this.productId,customer\u id:this.customerId,`被定义了,还是你的意思是别的?“我应该把所有的东西都放在有效负载对象中,而不是单独声明它们吗?”“因此,在末尾添加有效负载对象会让我认为customer\u id和product\u id会被发送两次”所以,通过输入payload+并在参数中使用它们,就像发送twitce,1通过参数,2通过payload对象。“这是正确的还是会产生错误?”即使您发送了多次,在大多数情况下也不会给出错误。非常感谢。