http get请求中的angular5获取错误

s5a0g9ez  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(326)

我正在学习angular5,我正在按照这个教程学习https://angular.io/tutorial
我是使用linux操作系统的php开发人员,我正在尝试从mysql数据库获取数据,但我得到了以下错误
得到http://localhost:4200/api/getusers 404(未找到)

有人能帮我解决这个问题吗?
请检查我的三个文件
1.proxy-config.json文件
2.用户服务.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 { }
webghufk

webghufk1#

默认情况下,angular应用程序在端口4200上运行,除非您显式更改它并且在同一端口上发出ajax请求 http://localhost:4200/api/getUsers .
我相信您打算在服务器运行的其他端口上发出http请求。

相关问题