如何在Angular 6中发送带文本的帖子请求

问题描述 投票:1回答:2

我有一个Api with Post方法,用于在DataBase中搜索关键字。我想在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#火

 [HttpPost]
 [Route("/search/{searchform}")]
    public ActionResult Search(string searchform)
    {
     var search = _context.SlideDB.Where(p => p.TextSlide.Contains(searchform));

        return Ok(search);
    }
angular typescript
2个回答
2
投票

您需要在组件中调用subscribe(),否则请求不会被调用

search() {
    this.http.post(this.Url + '/search/', this.text).subscribe((data)=>{
        return data;
    })

; }

还使用ngModel绑定输入文本

<input type="text" [(ngModel)]="text" class="form-control" placeholder="Search">

0
投票

您需要订阅您的帖子。

this.http.post(this.Url + '/search/', text).subscribe(result => console.log(result));

© www.soinside.com 2019 - 2024. All rights reserved.