【问题标题】:passing a parameter from Angular 7 to a PHP API将参数从 Angular 7 传递到 PHP API
【发布时间】:2018-11-06 02:00:29
【问题描述】:

我需要知道如何在 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 错误 HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK

【问题讨论】:

  • 我收到了这个错误 HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "localhost/backend/json/data_product.php/?id=14", ok: false, ...}
  • 试试这个{params:params}
  • 连接有效,因为你有一个 200,它与你发送的内容有关,尝试 params: params 并在后端再次检查你想要接收的内容与你发送的内容相同,或者你可以通过像这样的 URL 的 ID http.post(URL + /${productId})
  • 对不起,您需要在 /${productID} 上使用模板字符串,使用这个 ``

标签: php angular typescript angular7


【解决方案1】:

请参考 Angular 文档:https://angular.io/api/common/http/HttpClient#get

get(url: string, options: { headers?: HttpHeaders | { [header: 字符串]:字符串 |细绳[]; };观察?:“身体”; 参数?:Ht...)

应该是这样的:

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

在你的情况下。

【讨论】:

  • {params} 只是{params: params} 的简写,使用ECMAScript 2015 notation
  • 好吧,在这种情况下,我猜他使用的是 IE。也许这就是根本原因。
  • Typescript 被转译,所以它实际上转译为 {params: params} 反正
【解决方案2】:

我认为您需要在请求中传递标头,如下所示。也许这对您有帮助。

更新(id:数字,数据:任意){

让模型 = JSON.stringify(data);

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2014-03-04
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多