• 注册
  • 捐助

    请在小工具里添加二维码

    • 今日签到
    • 连续签到
  • Tom
    Tom
    今天 00:58
  • Tom
    Tom
    • 查看作者
    • Post parsing data to get data from server Angular

      Post and get data from server Angu

      import { HttpClientHttpHeaders,HttpParams,HttpClientModulefrom '@angular/common/http';

      handlePlayClick() {

          this.play_clicked = true;

          

          const params = new HttpParams()
          .set('choice'this.select)

             

          this.http.post(this.serverUrl,params)
          .subscribe(
              (val=> {

                  

                  console.log("POST call successful value returned in body"
                    val);
                    this.result = val
                    if (val.result === 'win')
                    this.personal_score +=1

                    

                    else if (val.result === 'lose')
                    this.computer_score +=1

                    this.total_played +=1
              },
              response => {
                  console.log("POST call in error"response);
              },
              () => {
                  console.log("The POST observable is now completed.");
              });
            this.select = '';
        }

      import HttpClientModule after BrowserModule

      @NgModule({
        imports: [
          BrowserModule,
          // import HttpClientModule after BrowserModule.
          HttpClientModule,
        ],
        declarations: [
          AppComponent,
        ],
        bootstrap: [ AppComponent ]
      })

      localserver need to add below code for localserver access

      //for localhost access
      app.use((reqresnext=> {
        res.header('Access-Control-Allow-Origin''*');
        next();
      });

    • 0
    • 0
    • 0
    • 46
    • 请登录之后再进行评论

      登录
    • 做任务
    • 实时动态
    • 返回顶部