无法发送POST请求。无法读取未定义的属性“ post”

时间:2019-06-01 13:15:25

标签: post angular7

因此,我在GitHub和StackOverflow上经历了大多数类似的问题。

my issue.component.ts文件中,我已将下拉菜单的值绑定到变量issueInformation。现在,我需要将此数据发送到服务器,并且我正在使用后请求。

这是我的issue.component.ts文件:

import { Component, OnInit, DoCheck } from '@angular/core';
import { IssuesService } from '../issues.service';
import { HttpClientModule } from '@angular/common/http';
import { Http } from '@angular/http';



@Component({
  selector: 'app-issue',
  templateUrl: './issue.component.html',
  styleUrls: ['./issue.component.css']
})
export class IssueComponent implements OnInit,  DoCheck {

issueInfo: any[] = [];
issueInformation: any;
    httpClient: any;
    http: any;
  constructor(private issue: IssuesService) { }
ngDoCheck(): void {
        console.log('issue:', this.issueInformation);
    }

  ngOnInit() {
    this.getIssues();
  }
    getIssues() {

        console.log('issue::', this.issueInformation);

        this.issue.allIssues().
    subscribe(
      data2 => {
          this.issueInfo = data2.Issues;

      },
      err => console.log(err),
      () => console.log('complete')
    );

  }


}

因为我是Angular的新手,所以我尝试了一个带有POST请求的示例,并将其添加到getIssues()函数之后的该文件中:

doPOST() {
  console.log("POST");
  let url = `${this.apiRoot}/post`;
  this.http.post(url, {moo:"foo",goo:"loo"}).subscribe(res => console.log(res.json()));
}

我还尝试将(change)="doPOST($event.target.value)"添加到HTML的下拉选择行中。

我得到的错误是:

AppComponent.html:1 ERROR TypeError: Cannot read property 'post' of undefined
    at IssueComponent.getIssues (issue.component.ts:30)
    at IssueComponent.ngOnInit (issue.component.ts:24)
    at checkAndUpdateDirectiveInline (core.js:24489)
    at checkAndUpdateNodeInline (core.js:35151)
    at checkAndUpdateNode (core.js:35090)
    at debugCheckAndUpdateNode (core.js:36112)
    at debugCheckDirectivesFn (core.js:36055)
    at Object.eval [as updateDirectives] (AppComponent.html:7)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:36043)
    at checkAndUpdateView (core.js:35055)

我尝试了以下操作: constructor{private issue: IssuesService,private http: Http} 但这对我不起作用。

我还尝试了将issues.service.ts文件中的doPOST()函数移位,但是无法在issue.component.ts文件中调用它。

这是issues.service.ts文件:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Response, Headers, RequestOptions} from '@angular/http';
import { Observable} from 'rxjs';
import { IssueComponent } from './issue/issue.component';


@Injectable({
  providedIn: 'root'
})


export class IssuesService {

  url = 'http://localhost:8080/issueTypes';
_baseUrl = 'http://localhost:8080';
  constructor(private http: HttpClient) { }

  allIssues(): Observable<any> {
    return this.http.get(this.url);
  }
 doPost() {
      console.log('POST');
      const url = '/api/issue';
      this.http.post(url, {moo: 'foo', goo: 'loo'}).subscribe(res => console.log(res.json()));
                                          }
}

有人可以告诉我怎么了吗?如果我从一开始就做错了,那么请告诉我如何将issueInformation的值发送回服务器?

1 个答案:

答案 0 :(得分:0)

花了整整一天时间试图解决这个问题之后,我找到了解决方案:

constructor(private issue: IssuesService, private httpClient: HttpClient) { }

添加此内容后,可以将POST请求与issue.component.ts放在同一文件中,如下所示:

postIssue() {
      this.httpClient.post('http://localhost:8080/uploadFile', 
      {issueInformation: this.issueInformation}).subscribe((data: any) => 
      console.log(data));
  }