Service can't reach post function
嘿(第一次在这里发帖,这样我可能会违反一些规则,告诉我是否这样做)
我正在尝试创建一个RESTAPI,但有一些问题。实际上,post函数不是在c_api上触发的,我不知道为什么getall()函数使用的get函数运行良好。
角度服务:
1 2 3 4 5 6 7 8 9 10 11 12 | public GetAll = (): Observable<Expertise[]> => { return this.http.get(this.actionUrl, '').map((response: Response) => <Expertise[]>response.json()); } public Add = (thingToAdd: Expertise): Observable<Expertise> => { thingToAdd.Id = 1; let toAdd = JSON.stringify(thingToAdd); console.log(toAdd); console.log(this.actionUrl); return this.http.post(this.actionUrl, toAdd, { headers: this.headers }).map((response: Response) => response.json()); } |
C·API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // GET api/values [HttpGet] public async Task<IEnumerable<Expertise>> Get() { try { System.Console.WriteLine("Test get all"); //var result = await cvService.Get("[email protected]"); return new List<Expertise>(); } catch (System.Exception ex) { logger.LogError(ex.Message, ex); throw; } } // POST api/values [HttpPost] public Expertise Post([FromBody]Expertise expertise) { try { System.Console.WriteLine("Test post"); context.Expertises.Add(expertise); context.SaveChanges(); logger.LogInformation("New expertise added !"); return expertise; } catch (System.Exception ex) { logger.LogError(ex.Message, ex); throw; } } |
专业知识(EF模型):
1 2 3 4 5 6 7 8 9 | public class Expertise { [Key] public int Id { get; set; } public string Name { get; set; } public ICollection<ExpCV> CVs { get; set; } } |
如果有人想把这个服务和我的API"链接"起来,我会一直坚持下去。
提前谢谢你
由于您的服务返回一个
看看我设置的这辆破车:
plunker: https://plnkr.co/edit/r6aBaB3BjJzdIS4myAd8?p=preview
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Component({ selector: 'app', template: ` <button (click)="searchNoSub()">searchNoSub</button> <button (click)="search()">search</button> ` }) export class App { constructor(private searchService:SearchService) { } search(){ this.searchService.search().subscribe(console.log,console.log) } searchNoSub(){ this.searchService.search(); } } |
搜索服务:
1 2 3 4 5 6 7 8 9 10 11 | @Injectable() export class SearchService { constructor(private http: Http) {} search(term: string) { // get artists named john from spotify API return this.http.get('https://api.spotify.com/v1/search?q=john&type=artist') .map((response) => response.json()); } } |
现在,在chrome中打开devtools和
单击
简而言之,您需要订阅可观察到的请求。