简体   繁体   English

在Angular中的POST请求之后调用GET请求

[英]calling GET request after POST request in Angular

I want to call make a GET request after a POST request using Angular. 我想使用Angular在POST请求之后调用GET请求。

Here is the code I have so far, is it the good solution? 这是我到目前为止的代码,这是一个好的解决方案吗?

   update(){
     this.http.post<any>("/ssservice", "products=" + body, options)
       .suscribe({
         complete: () => {
           this.http.get<Product[]>("/ssservice")
             .suscribe({
               (data: Product[]) => products.push(...data),
               err => console.log(err)})
         }
       });
    }

Any help is greatly aprreciated. 非常感谢任何帮助。

I think @PardeepJain is telling 我认为@PardeepJain告诉

update(){
  this.http.post<any>("/ssservice", "products=" + body, options)
    .suscribe({
      complete: () => {
        this.getProduct()
      },
    });
}

getProduct () {
  this.http.get<Product[]>("/ssservice")
    .subscribe({
      (data: Product[]) => products.push(...data),
       err => console.log(err)})
}

Yes off course this is the good approach as http is asynchronous call so in that case better to call any fucntion/code within the success block of http. 是的,当然,这是个好方法,因为http是异步调用,因此在这种情况下最好调用http成功块内的任何功能/代码。

But try to break your code as much as you can. 但是,请尽量破坏您的代码。 As in your use case you can call another function and call get request within there like this - 与您的用例一样,您可以像这样调用另一个函数并在其中调用get请求-

update(){
     this.http.post<any>("/ssservice", "products=" + body, options)
       .suscribe(
         complete: () => {
           this.anotherGetReuqest();
         });
    }

anotherGetReuqest() {
this.http.get<Product[]>("/ssservice")
   .subscribe({(data: Product[]) => products.push(...data),
    err => console.log(err)})
}

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

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