problem with the ionic service code [closed]

1

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {  Headers } from '@angular/http';
import 'rxjs/add/operator/map';

let apiUrl = "http://localhost/PHP-Slim-Restful/api/";

@Injectable()
export class AuthServiceProvider {

 

  constructor(public http: HttpClient,
  ) {
    console.log('Hello AuthServiceProvider Provider');
  }
 
  postData(credentials, type) {

    return new Promise((resolve, reject) => {
       let headers = new Headers();

       return this.http.post(apiUrl + type, JSON.stringify({credentials}),{ headers: headers })
        .subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    });

  }

}

{headers: headers} < - that piece of line throws me wrong.

    
asked by Claudio Navarrete 18.01.2018 в 13:51
source

2 answers

0

It is necessary to adjust the code like this:

  
  • Change Headers by HttpHeaders
  •   
  • Remove rxjs/add/operator/map
  •   
  • Add typing
  •   
  • Implement HttpHeaders
  •   
  • Remove header Access-Control-Allow-Origin since it is not a header from the client, but from the server
  •   
  • Remove the .json() from the response since the response is already a JSON
  •   
  • To access the property apiUrl from a method it is necessary to use the this to be able to access, this.apiUrl
  •   
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Injectable } from "@angular/core";

@Injectable()
export class AuthServiceProvider {
  private apiUrl: string = "http://localhost/PHP-Slim-Restful/api/";

  constructor(public http: HttpClient) {}

  public postData(credentials, type): Promise<any> {
    let httpOptions = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
        Authorization: 'Bearer TOKEN'
      })
    };

    return new Promise((resolve, reject) => {
      this.http.post(this.apiUrl + type, JSON.stringify(credentials), httpOptions).subscribe(
        res => {
          resolve(res);
        },
        err => {
          reject(err);
        }
      );
    });
  }
}

Review the official HttpClient documentation

    
answered by 18.01.2018 / 17:12
source
0

Try this way, defining Headers and assigning the content-type , since you send a Json and it should not go empty the Header either.

Code:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {  Headers } from '@angular/http';
import 'rxjs/add/operator/map';



@Injectable()
export class AuthServiceProvider {
  private apiUrl = "http://localhost/PHP-Slim-Restful/api/";
  private headers = new Headers();

  constructor(public http: HttpClient,
  ) {
    console.log('Hello AuthServiceProvider Provider');
  }

  postData(credentials, type): Promise<any> {

    this.headers.append('Content-Type', 'application/json');
    this.headers.append('Access-Control-Allow-Origin', '*');
    let options = new RequestOptions({ headers: this.headers });
    return new Promise((resolve, reject) => {
     this.http.post(apiUrl + type, JSON.stringify(credentials),options)
        .subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    });

  }

}
    
answered by 18.01.2018 в 14:34