我需要知道如何在angular 7和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});
}
}
但是我得到了这个错误core.js:12584 ERROR HttpErrorResponse {headers:HttpHeaders,status:200,statusText:“OK
请参阅Angular doc:https://angular.io/api/common/http/HttpClient#get
get(url:string,options:{headers?:HttpHeaders | {[header:string]:string | string [];};观察?:“body”; params?:Ht ......)
应该是这样的:
this.http.get('http://localhost/backend/json/data_product.php/', { params: params });
在你的情况下。
我认为您需要在下面的请求中传递标题。可能这对您有所帮助。
update(id:number,data:any){
let model = JSON.stringify(data);
let headers = new Headers({'Content-Type':'application / json'});
let options = new RequestOptions({headers:headers});
返回this._http.put('http://localhost/backend/json/data_product.php/'+ id,model,options);
}