从Angular 2发送restful请求 [英] send restful request from Angular 2

查看:152
本文介绍了从Angular 2发送restful请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular登录应用程序,我在java中有一个宁静的web服务,应该在数据库中搜索用户。请帮助我如何从角度向此服务发送请求。提前谢谢

I have an Angular login app and also I have a restful webservice in java which should search for an user in database . Please, help me how to send request from angular to this service. thanks in advance

推荐答案

首先,创建一个角度服务,它将作为您的webservice和angular应用程序之间的连接器。有关创建服务的信息,请参阅
在你的service.ts中添加代码,如

Firstly, create a service in angular which will serve as a connector between your webservice and angular app. For creating service.ts follow this. In your service.ts add codes like

searchService() {
    const url = 'yourURL' + 'your api url';
    return this.http.get(url).map((res: Response) => res.json()); 
}//for get request



searchService(data) {
      const url = 'yourURL' + 'your api url';
      return this.http.post(url, data, this.jwt()).map((response: Response) => response.json());
  }//for post request

现在在.ts文件中调用此服务函数您将service.ts导入所需的.ts文件,如:

Now call this service function in your .ts file after you import service.ts into your required .ts file like:

callSearchService() {
    this._service.searchService(data).subscribe(searchData => {
        console.log(searchData);//your API response
    });
}

这可以帮助您将角度应用程序嵌入到数据库中。
请询问您是否有任何疑问。

This much should help your, for intracting your angular app to your database. Please ask if you got any query.

这篇关于从Angular 2发送restful请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆