Angular 7 HttpClient POST-错误请求
再次学习该站点的教程,我在winamp中创建了一个数据库,其中包含一个字段{id:id,name:name}的表,并使用Symfony4对该表进行了2次查询: 1列出英雄的请求。 2创建英雄的请求 从Angular 7开始执行,查询1可以完美地运行route/listerhereoes 从Angular 7开始执行,查询2不起作用,它返回错误405 route/Ajuterhero。不管是从邮递员那里发起的,这个查询都是有效的 我找不到任何文档向我解释这个错误,我在这几天中都遇到了这个错误。请给我一首曲子 下面是两个类的副本:heromes.service.ts和component3.component.tsAngular 7 HttpClient POST-错误请求,post,symfony4,angular7,Post,Symfony4,Angular7,再次学习该站点的教程,我在winamp中创建了一个数据库,其中包含一个字段{id:id,name:name}的表,并使用Symfony4对该表进行了2次查询: 1列出英雄的请求。 2创建英雄的请求 从Angular 7开始执行,查询1可以完美地运行route/listerhereoes 从Angular 7开始执行,查询2不起作用,它返回错误405 route/Ajuterhero。不管是从邮递员那里发起的,这个查询都是有效的 我找不到任何文档向我解释这个错误,我在这几天中都遇到了这个错误。请给
// heroes.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import { HttpErrorHandler, HandleError } from './http-error-handler.service';
import { Hero } from '../assets/Structure';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({ providedIn: 'root' })
export class HeroesService {
heroesUrl = 'http://heroes/';
private handleError: HandleError;
constructor(private http: HttpClient, httpErrorHandler: HttpErrorHandler) {
this.handleError = httpErrorHandler.createHandleError('HeroesService');
}
getHeroes$(): Observable<Hero[]> {
return this.http.get<Hero[]>(`${this.heroesUrl}listerHeroes`, httpOptions);
}
addHero(hero: Hero): Observable<Hero> {
return this.http
.post<Hero>(`${this.heroesUrl}ajouterHero`, hero, httpOptions)
.pipe(catchError(this.handleError('addHero', hero)));
}
}
// component3.component.ts
import { Component, OnInit } from '@angular/core';
import { HeroesService } from '../heroes.service';
import { Hero } from '../../assets/Structure';
@Component({
selector: 'app-component3',
templateUrl: './component3.component.html',
styleUrls: ['./component3.component.css']
})
export class Component3Component implements OnInit {
heroes: Hero[];
editHero: Hero;
constructor(private heroesService: HeroesService) {}
ngOnInit() {
this.heroesService.getHeroes$().subscribe(res => (this.heroes = res));
}
addHero(name: string): void {
name = name.trim();
console.log('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF C3A name =', name);
if (!name) {
return;
}
const newHero: Hero = { 'id': 0, 'name': name } as Hero;
this.heroesService.addHero(newHero).subscribe(hero => {
console.log('GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG C3B hero= ', hero);
this.heroes.push(hero);
});
}
}
我找到了解决办法:
阻塞被列为后端Symfony4,它拒绝预查询选项。有必要安装和配置bundle nelmio,使请求能够顺利运行