Ionic 4 Angular 7-HTTPClient.post()不返回任何响应

时间:2019-02-21 23:26:05

标签: angular ionic-framework angular7 ionic4

我正在尝试对Web服务执行HTTPClient.post(),但未收到任何响应。

我尝试通过邮递员执行服务呼叫,并且收到响应。

我尝试启用和禁用CORS,但没有区别。

以下是我的服务。

authentication.service.ts

    import { Platform } from '@ionic/angular';
    import { Injectable } from '@angular/core';
    import { Storage } from '@ionic/storage';
    import { BehaviorSubject } from 'rxjs';
    import { HttpClient, HttpHeaders, HttpRequest, HttpParams, HttpResponse } from '@angular/common/http';

    const TOKEN_KEY = 'auth-token';

    @Injectable({
      providedIn: 'root'
    })
    export class AuthenticationService {

      constructor(private storage: Storage, private plt: Platform,private http: HttpClient) { }

       login() {
        const url ="http://localhost/login";
        const  params = JSON.stringify(new  HttpParams().set('Login', "testuser").set('Password', "test").set('Mobile', "true"));
        const httpOptions = {
          headers: new HttpHeaders({
            'Accept': 'application/json',
            'Content-Type':  'application/x-www-form-urlencoded'
          })
        };

       this.http.post(url,params,httpOptions).subscribe((response) => {
          console.log(response);
        }, error => {
          console.log(error);
       });
      }
    }

我尝试如下添加标题选项,但没有帮助。

    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
    'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
    'Access-Control-Allow-Credentials': true 

我尝试通过添加responseType:'json'来进行尝试,但是无法编译。

我在做什么错了?

0 个答案:

没有答案