问题
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