当前位置: 首页 > 知识库问答 >
问题:

错误TS2345:“{headers:HttpHeaders;}”类型的参数不能分配给“RequestOptionSargs”类型的参数

卓雅达
2023-03-14

这是我运行Ionic Serve时得到的结果。我使用的是ionic4版本的Angular 8。

sendPostRequest() {

    let headers = new HttpHeaders();

    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'text/plain');
    let options = {
        headers: headers
    }


    /*
         if (this.authService.checkAuth()) {
             const token = 'something';
             headers = headers.set('Authorization', `Bearer ${token}`);
         }
      const authReq = req.clone({ headers });*/


    let postData = {

        "username": "Customer004",
        "email": "customer004@email.com",
        "password": "0000252525",
        "gender": "male",
    }

    /*this.http.post("http://localhost/android/Api.php?apicall=signup", postData, requestOptions)
      .subscribe(data => {
        console.log(data['_body']);
       }, error => {
        console.log(error);
      });*/


    var options = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        })
    };


    this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
        .subscribe(data => {
            console.log(data);
        });

}
{
  "name": "registration",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/http": "^7.2.16",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ionic-native/core": "^5.0.0",
    "@ionic-native/splash-screen": "^5.0.0",
    "@ionic-native/status-bar": "^5.0.0",
    "@ionic/angular": "^4.7.1",
    "core-js": "^2.5.4",
    "rxjs": "~6.5.1",
    "tslib": "^1.9.0",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.801.2",
    "@angular-devkit/build-angular": "~0.801.2",
    "@angular-devkit/core": "~8.1.2",
    "@angular-devkit/schematics": "~8.1.2",
    "@angular/cli": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@ionic/angular-toolkit": "^2.1.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^8.9.5",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "An Ionic project"
}

共有1个答案

曾奇略
2023-03-14

您应该进行导入:从'@angull/Http'导入{Http,Headers,RequestOptions};

来自http的DI注入是可以的。然后简单地使用RequestOptions,如下所示:

sendPostRequest() {
    let headers = new Headers({'Content-Type': 'application/json'});
    // headers.append('Content-Type', 'text/plain');
    // I didn't understand if you need this append.

    let options = new RequestOptions({ headers: headers });

    const postData = {
        "username": "Customer004",
        "email": "customer004@email.com",
        "password": "0000252525",
        "gender": "male",
    }

    this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
        .subscribe(data => {
            console.log(data);
        });
}
 类似资料: