Ionic 4 Angular 7 - HTTPClient.post() is not returning any response

夙愿已清 提交于 2019-12-13 03:46:33

问题


I am trying to do a HTTPClient.post() to a web service but I am receiving no response.

I tried executing the service call through post man and I am receiving a response.

I have tried enabling and disabling CORS but there is no difference.

Below is my service.ts

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);
       });
      }
    }

I have tried adding header options as below but none have helped.

    '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 

I have tried by adding responseType: 'json' but that doesn't compile.

What am I doing wrong?

来源:https://stackoverflow.com/questions/54817814/ionic-4-angular-7-httpclient-post-is-not-returning-any-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!