如何在Angular的http发布请求中传递变量/参数(包括斜杠)?

时间:2020-08-23 19:09:20

标签: angular angular-httpclient

在以角度的http放置请求期间,我正在url中传递变量或参数,而我正在传递的变量为(x / y)形式。在传递此类变量时,由于此变量形式(x / y),我定位的网址没有被点击。我认为是因为在传递此类变量时url正在更改。简而言之,

我要发送放置请求的URL http://localhost:5000/api/Transmission/{variable}/deleteDashboardAlert

但是此请求发送到的网址 http://localhost:5000/api/Transmission/x/y/deleteDashboardAlert

以下是我正在调用并将参数传递给->

的函数
deleteAlert(transmissionId, alertTypeId, description) {
 return this.http .put(
  `transmission/${transmissionId}/${alertTypeId}/${description}/deleteDashboardAlert`,
  {}
 );
}

所以基本上在上面的函数中,描述变量会造成此问题

2 个答案:

答案 0 :(得分:1)

您可以使用encodeURIComponent来编码variable

deleteAlert(transmissionId, alertTypeId, description) {
 const variable = encodeURIComponent(`${transmissionId}/${alertTypeId}/${description}`);
 return this.http.put(`transmission/${variable}/deleteDashboardAlert`, {});
}

答案 1 :(得分:0)

您可以像下面那样在端点中添加/添加任何动态值。

demo.service.ts

编辑:添加了encodeURIComponent以对传递的参数进行编码。

@Injectable({
  providedIn: 'root'
 })
 export class DemoService {
    baseUrl = 'http://localhost:5000';
    constructor(private http: HttpClient) {}

   getDemoData(someValue) {
     const endcodedValue = encodeURIComponent(someValue);
     const url = `${this.baseUrl}/api/Transmission/${endcodedValue}/deleteDashboardAlert`;
     return this.http.get(url);
   }

   getAnOtherDemoData(someValue1, someValue2 ) {
     const endcodedValue = encodeURIComponent(`${someValue1}/${someValue2}`);
     const url = `${this.baseUrl}/api/Transmission/${endcodedValue}/deleteDashboardAlert`;
     return this.http.get(url);
   }
 }