angularjs的请求数据的方式
在 app.module.ts 中引入 HttpClientModule 并注入
import {HttpClientModule} from '@angular/common/http'; imports: [ BrowserModule, HttpClientModule ]
Angular get 请求数据
在用到的地方引入 HttpClient 并在构造函数声明
import {HttpClient} from "@angular/common/http"; constructor(public http:HttpClient) { }
get 请求数据
var api = "http://a.itying.com/api/productlist"; this.http.get(api).subscribe(response => { console.log(response); });
Angular post 提交数据
在用到的地方引入 HttpClient、HttpHeaders 并在构造函数声明 HttpClient
import {HttpClient,HttpHeaders} from "@angular/common/http"; constructor(public http:HttpClient) { }
post 提交数据
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; var api = "http://127.0.0.1:3000/doLogin"; this.http.post(api,{username:'张三',age:'20'},httpOptions).subscribe(response => { console.log(response); });
Angular Jsonp 请求数据
在 app.module.ts 中引入 HttpClientModule、HttpClientJsonpModule 并注入
import {HttpClientModule,HttpClientJsonpModule} from '@angular/common/http'; imports: [ BrowserModule, HttpClientModule, HttpClientJsonpModule ]
在用到的地方引入 HttpClient 并在构造函数声明
import {HttpClient} from "@angular/common/http"; constructor(public http:HttpClient) { }
jsonp 请求数据
var api = "http://a.itying.com/api/productlist"; this.http.jsonp(api,'callback').subscribe(response => { console.log(response); });
Angular 中使用第三方模块 axios 请求数据
安装 axios
cnpm install axios --save
用到的地方引入 axios
import axios from 'axios'; axios.get('/user?ID=12345') .then(function (response) { // handle success console.log(response); }) .catch(function (error) { // handle error console.log(error); }) .then(function () { // always executed });
封装service作为http服务
import { Injectable } from '@angular/core'; import axios from 'axios'; @Injectable({ providedIn: 'root' }) export class HttpserviceService { constructor() { } axiosGet(api){ return new Promise((resolve,reject)=>{ axios.get(api) .then(function (response) { // handle success resolve(response) }); }) } }
相关推荐
QiaoranC 2020-09-25
颤抖吧腿子 2020-09-04
liduote 2020-06-16
阿斌Elements 2020-06-11
xxuncle 2020-06-05
ChinaGuanq 2020-06-05
wanghongsha 2020-03-26
csm0 2020-03-05
shyoushine 2020-02-25
electronvolt 2020-02-12
jsonwoo 2020-01-20
ZadarrienChina 2020-01-07
wwwxuewen 2020-01-04
dynsxyc 2020-01-03
liangjielaoshi 2019-12-27
bowean 2019-12-27
wwwxuewen 2019-12-25
liwusen 2019-12-16
颤抖吧腿子 2019-12-16