0
votes

I need to know how to pass parameters between angular 7 and a PHP API

 import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  constructor(private http: HttpClient) { }
  getUsers() {
    return this.http.get('http://localhost/backend/json/data_products.php');
  }
  getProduct(productId) {
    const params = new HttpParams().set('id', productId);
    return this.http.get('http://localhost/backend/json/data_product.php/', {params});
  }
}

but I got this error core.js:12584 ERROR HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK

2
I got this error HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "localhost/backend/json/data_product.php/?id=14", ok: false, …} - Yosvani Valdes
Try this instead {params:params} - User3250
connection works because you got a 200, its something related about what you sending, try params: params and check again on the backend what you want to recieve its the same you sending, or you can pass the ID to the URL like this http.post(URL + /${productId}) - Sergi
sorry, you need to use template string on /${productID} using this `` - Sergi

2 Answers

0
votes

Please refer to Angular doc: https://angular.io/api/common/http/HttpClient#get

get(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...)

It should be like:

this.http.get('http://localhost/backend/json/data_product.php/', { params: params });

in your case.

-1
votes

I think you need to pass header in request like below.May be this is help you.

update(id: number, data: any){

let model = JSON.stringify(data);

let headers = new Headers({ 'Content-Type': 'application/json' });

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

return this._http.put( 'http://localhost/backend/json/data_product.php/'+id,model, options);

}