怎么在Angular中實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)怎么在Angular中實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供贛榆網(wǎng)站建設(shè)、贛榆做網(wǎng)站、贛榆網(wǎng)站設(shè)計(jì)、贛榆網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、贛榆企業(yè)網(wǎng)站模板建站服務(wù),十年贛榆做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

使用 Angular 請(qǐng)求數(shù)據(jù)的時(shí)候,需要引入 HttpModule 模塊,如果使用的 jsonp 模式的話,則需要另外引入 JsonpModule 模塊

import { HttpModule, JsonpModule } from '@angular/http'

然后在當(dāng)前模塊中的 imports 內(nèi)進(jìn)行注冊(cè)

imports: [
 HttpModule,
 JsonpModule
],

注冊(cè)以后就可以在組件文件當(dāng)中引入相對(duì)應(yīng)的方法來(lái)進(jìn)行數(shù)據(jù)請(qǐng)求了

import { Http, Jsonp } from '@angular/http'

然后在當(dāng)前組件的構(gòu)造函數(shù)當(dāng)中進(jìn)行注入以后就可以使用了

constructor(private http: Http, private jsonp: Jsonp) { }

使用如下,一個(gè)簡(jiǎn)單的 get 請(qǐng)求

// 進(jìn)行注入,拿到相對(duì)應(yīng)的方法
constructor(private http: Http, private jsonp: Jsonp) { }
public list: any = []
// 請(qǐng)求數(shù)據(jù)
getData() {
 let url = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1'
 let _this = this
 this.http.get(url).subscribe((data) => {
  _this.list = JSON.parse(data['_body'])['result']
  console.log(_this.list)
 })
}

前臺(tái)進(jìn)行渲染即可

<button (click)="getData()">get 請(qǐng)求數(shù)據(jù)</button>
<ul>
 <li *ngFor="let item of list">
  {{item.title}}
 </li>
</ul>

JSONP 請(qǐng)求數(shù)據(jù)

注意區(qū)分與 get 請(qǐng)求的區(qū)別,使用如下

// 請(qǐng)求數(shù)據(jù)
jsonpData() {
 let url = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSONP_CALLBACK'
 let _this = this
 this.jsonp.get(url).subscribe((data) => {
  _this.list = data['_body']['result']
  console.log(_this.list)
 })
}
// 渲染
<button (click)="jsonpData()">jsonp 請(qǐng)求數(shù)據(jù)</button>
<ul>
 <li *ngFor="let item of list">
  {{item.title}}
 </li>
</ul>

不同點(diǎn)

請(qǐng)求的 url 參數(shù)結(jié)尾必須要添加指定的回調(diào)函數(shù)名稱 &callback=JSONP_CALLBACK

請(qǐng)求的方式變?yōu)?this.jsonp.get(url)

請(qǐng)求后得到的數(shù)據(jù)格式不統(tǒng)一,需要自行進(jìn)行調(diào)整

POST 請(qǐng)求

與 GET 的請(qǐng)求方式有些許不同,首先需要引入請(qǐng)求頭 { Headers }

import { Http, Jsonp, Headers } from '@angular/http'

然后來(lái)對(duì)請(qǐng)求頭進(jìn)行定義,需要先實(shí)例化 Headers

private headers = new Headers({'Content-Type': 'application/json'})

最后在提交數(shù)據(jù)的時(shí)候帶上 Headers 即可

postData() {
 let url = 'http://localhost:8080/login'
 let data = {
  "username": "zhangsan",
  "password": "123"
 }
 this.http.post(
  url,
  data,
  {headers: this.headers}
 ).subscribe((data) => {
  console.log(data)
 })
}

看完上述內(nèi)容,你們對(duì)怎么在Angular中實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

標(biāo)題名稱:怎么在Angular中實(shí)現(xiàn)數(shù)據(jù)請(qǐng)求-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.muchs.cn/article16/deoidg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站制作微信小程序、網(wǎng)站排名、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)