简体   繁体   English

如何使用 Angular 中的删除方法在正文中传递数据?

[英]How can I use the delete method in Angular to pass data in body?

I have the below AWS lambda function which I have created with the intention of defining soft deleting and hard deleting my product.我创建了以下 AWS lambda function,目的是定义软删除和硬删除我的产品。

 exports.handler = async (event) => { const product = JSON.parse(event.body); let sql; let msg; let values; if (product.action == 'delete') { sql = "UPDATE product_table SET isDeleted=1, deletedon=? WHERE product_id=?"; msg = 'The product has been sucessfully deleted'; values = new Date(); } else if (product.action == 'restore') { sql = "UPDATE product_table SET isDeleted=0, deletedon=? WHERE product_id=?"; msg = 'The product has been successfully restored'; values = null; } try { const data = await new Promise((resolve, reject) => { connection.getConnection(function (err) { if (err) { reject(err); } connection.query(sql, [values, product.product_id], function (err, result) { if (err) { console.log("Error->" + err); reject(err); } resolve(result); }); }); }); return { statusCode: 200, body: JSON.stringify(msg) }; } catch (err) { return { statusCode: 400, body: JSON.stringify(err.message) }; } };

The idea is to use a single delete method from API and pass id and action values in the JSON object to goggle between delete and restore.这个想法是使用 API 中的单个删除方法,并在 JSON object 中传递 id 和 action 值来在删除和恢复之间进行切换。

I was able to get this working on Postman, but not from my Angular Application.我能够在 Postman 上使用它,但不是从我的 Angular 应用程序中。 I am passing the values sam as I would do with POST method.我像使用 POST 方法一样传递值。 Can you please help me on what I am missing?你能帮我解决我所缺少的吗?

Below is the angular code:下面是 angular 代码:

 deleteProductData(product_id:number,action:string) { const header: HttpHeaders = new HttpHeaders().append('Content-Type', 'application/json; charset=UTF-8') const httpOptions = { headers: header, body:JSON.stringify({ "product_id": product_id,"action":action }) }; return this.http.delete<any>(this.productGetUrl, httpOptions); }

Found the issues.发现了问题。 My configurations were incorrect.我的配置不正确。

I was trying to send the data for the delete method inside a 'body', not knowing that the delete method only supported queryStringParameters or pathParameters.我试图在“正文”中发送删除方法的数据,但不知道删除方法只支持 queryStringParameters 或 pathParameters。

I was able to fix the issue by refactoring my backend similar to how I configured a GET request.我能够通过重构我的后端来解决这个问题,类似于我配置 GET 请求的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM