我有一个带有Post方法的Api,它可以在数据库中搜索关键字。我想在Angular中创建搜索表单。我尝试了这段代码,但是没有任何效果。我应该如何以正确的角度发送文本?我现在有一个错误Http failure response for https://localhost:44359/search/: 404 Not Found
HTML
<div class="form-group">
<input type="text" [(ngModel)]="text" class="form-control" placeholder="Search" name="text" >
<button type="submit" class="btn btn-default" (click)="search()"
>Submit</button>
打字稿
export class TextSlidesComponent {
public text: string;
Url: string = "";
public SlidesFinded : any [] ;
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.Url = baseUrl;
}
search(text) {
this.http.post(this.Url + 'search/', this.text).subscribe(result =>
console.log(result));
}
}
C#Api
[HttpPost]
[Route("/search/{searchform}")]
public ActionResult Search(string searchform)
{
var search = _context.SlideDB.Where(p => p.TextSlide.Contains(searchform));
return Ok(search);
}
答案 0 :(得分:2)
您需要在组件内部调用subscribe(),否则请求将不会被调用
User
; }
还使用ngModel绑定输入文本
search() {
this.http.post(this.Url + '/search/', this.text).subscribe((data)=>{
return data;
})
答案 1 :(得分:0)
您需要订阅您的帖子通话。
this.http.post(this.Url + '/search/', text).subscribe(result => console.log(result));