Angular5在HTTP GET请求中获取错误

时间:2018-07-04 06:30:26

标签: php mysql angular

我正在学习angular5,并且正在按照本教程进行学习 https://angular.io/tutorial

我是使用Linux OS的PHP开发人员,我试图从mysql数据库中获取数据,但出现错误

获取http://localhost:4200/api/getUsers 404(未找到) enter image description here 有人可以帮我解决这个问题吗?

请检查我的三个文件的代码

1.proxy-config.json

2.user.service.ts

3.app-routing.module.ts


/*proxy-config.json*/
{
  "/api": {
  "target": "http://localhost:4200",
  "secure": false,
  "pathRewrite": {"^/api" : ""}
  }
}

/*user.service.ts*/
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

import { User } from './user';
import { USERS } from './mock-users';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
  providedIn: 'root'
})
export class UserService {
	
  private usersUrl = 'api/getUsers';  // URL to web api

  constructor(private http: HttpClient) { }

  /** GET users from the server */
  getUsers (): Observable<User[]> {
    return this.http.get<User[]>(this.usersUrl)
      .pipe(
        tap(users => this.log(`fetched users`)),
        catchError(this.handleError('getUsers', []))
      );
  }

  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }

  /** Log a HeroService message with the MessageService */
  private log(message: string) {
    //this.messageService.add('HeroService: ' + message);
  }
  
}

/*app-routing.module.ts*/
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
import { UsersComponent } from './users/users.component';

const routes: Routes = [
  {
    path: 'users',
    component: UsersComponent
  },    
];

@NgModule({
  imports: [RouterModule.forRoot(routes),
  			CommonModule
  			],
  exports: [RouterModule],
  declarations: []
})

export class AppRoutingModule { }

1 个答案:

答案 0 :(得分:1)

默认情况下,除非您显式更改角度应用程序并且它在同一端口http://localhost:4200/api/getUsers上发出AJAX请求,否则默认情况下该应用程序将在端口4200上运行。

我相信您打算在服务器正在运行的其他端口上发出HTTP请求。