简体   繁体   English

如何在角度7中从数据库中获取数据?

[英]how to fetch data from the database in angular 7?

I am having while fetching data from the database using PHP fetch API. 我在使用PHP fetch API从数据库中获取数据时遇到问题。 below PHP fetch API working fine with the react App but giving error using fetching using angular. 下面的PHP fetch API与react App可以正常工作,但是使用angular获取时会出错。 here is my complete code with PHP fetch API. 这是我使用PHP fetch API的完整代码。

app.component.ts : app.component.ts

import { Component } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
   constructor(private httpClient: HttpClient){}
  name = 'My App';
 students:any = [];
  baseUrl='http://veomit.com/test/zend/api/fetch.php';
  get_products(){
        this.httpClient.get(this.baseUrl).subscribe((res :{})=>{
        console.log(res);
        this.students = res;
        });
    }
}

app.component.html : app.component.html

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>
<button (click)="get_products()">GET Data</button>
<ul>
      <li *ngFor="let student of students" >
        userId: {{student.name}}
        Hellotitle: {{student.department}}
        body: {{student.marks}}
      </li>
    </ul> 

php fetch API - fetch.php PHP提取API-fetch.php

<?php 

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Request-Headers: GET,POST,OPTIONS,DELETE,PUT");
$con = mysqli_connect("mysql1004.mochahost.com","a310387_task_for","task_force","a310387_task_force");
$ar=array();

     $query=mysqli_query($con,"select * from student");
     while ($rows=mysqli_fetch_array($query)) {
        $ar[]=$rows;
     }
 echo json_encode($ar);

 ?>
```
Below is my console error:

    HttpErrorResponse {headers: {…}, status: 0, statusText: "Unknown Error", url: "http://veomit.com/test/zend/api/fetch.php"…}
    error: ProgressEvent
    isTrusted: true
    __proto__: ProgressEvent
    headers: HttpHeaders
    headers: Array[0]
    lazyUpdate: null
    normalizedNames: Array[0]
    __proto__: HttpHeaders
    message: "Http failure response for http://veomit.com/test/zend/api/fetch.php: 0 Unknown Error"
    name: "HttpErrorResponse"
    ok: false
    status: 0
    statusText: "Unknown Error"
    url: "http://veomit.com/test/zend/api/fetch.php"
    __proto__: HttpErrorResponse

plz first generate services from this command ng g service service_name then constuct a function -- 请先从此命令生成服务,然后再调用service service_name,然后构造一个函数-

  getAllData(url, headerData = {}){ const getUrl = this.apiUrl + url; //let header:any={'authtoken':this.token}; return this.http.get(getUrl, { headers: headerData }) .map(result => result) .catch(this.handleError) } 

then in app componen.ts 然后在应用程序组件中

 get_product(){ this.service_name.getAllData(this.url).subscribe( (response: any) => { if (response.status == true) { } else { } }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM