zoukankan      html  css  js  c++  java
  • Angular中的数据请求 内置模块HttpClient实现(get post jsonp 以及第三方模板axios请求数据

    封装服务:

    httpservice.service.ts

    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){
            resolve(response)
          });
        });
      }
    }

    在app.module.ts中使用:

    import{HttpserviceService}from "./services/httpservice.service";

    providers: [HttpserviceService],

    2.

    app.module.ts

    //引入HttpClientModule
    import{HttpClientModule,HttpClientJsonpModule}from "@angular/common/http";
    
      imports: [
        HttpClientModule,
        HttpClientJsonpModule
      ],

    http.component.ts

    import { Component, OnInit } from '@angular/core';
    //当作一个服务:
    import { HttpClient, HttpHeaders } from "@angular/common/http";
    import { from } from 'rxjs';
    
    
    //使用服务里面的axios获取数据:
    import{HttpserviceService} from "../../services/httpservice.service";
    @Component({
      selector: 'app-http',
      templateUrl: './http.component.html',
      styleUrls: ['./http.component.scss']
    })
    export class HttpComponent implements OnInit {
      public list: any[] = [];
      constructor(public http: HttpClient,public httpService:HttpserviceService) { }
    
      ngOnInit() {
      }
      getData() {
        alert("执行get请求");
        let api = "http://a.itying.com/api/productlist";
        this.http.get(api).subscribe((response: any) => {
          console.log(response);
          this.list = response.result;
        })
      }
    
      doLogin() {
        const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }
        //存在跨域:
        var api = "";
        this.http.post(api, { "username": "张三", "age": 20 }, httpOptions).subscribe((response) => {
    
        });
        alert("执行");
      }
    
      getJsonpData() {
    
        //jsonp请求,服务器必须的支持jsonp:
    
        let api = "http://a.itying.com/api/productlist";
        this.http.jsonp(api, 'callback').subscribe((response)=>{
          console.log(response);
        });
      }
    
      getAxiosData(){
        let api = "http://a.itying.com/api/productlist";
        this.httpService.axiosGet(api).then((data)=>{
          console.log(data);
        });
      }
    }

    http.component.html

    <p>
        new works
    </p>
    <button (click)="getData()">get请求的数据</button>
    
    <br/>
    <ul>
        <li *ngFor="let item of list">
            {{item.title}}
        </li>
    </ul>
    
    <br/>
    <button (click)="doLogin()">post提交数据</button>
    
    <br/>
    <button (click)="getJsonpData()">jsonp请求</button>
    
    <br/>
    <button (click)="getJsonpData()">通过第三方模块获取的请求的数据</button>

  • 相关阅读:
    事务(进程 ID 133)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品的解决方案
    Waiting for cache lock
    Win11系统将软件安装在C盘后,打开软件会有无法正常读写C盘下文件的问题
    SharePoint 2010 微软学习教程
    Oracle 远程连接配置文件
    如何优化操作大数据量数据库(几十万以上数据)(二。改善SQL语句)
    SQL BI Microsoft MSDN
    Oracle 错误:ORA06413: Connection not open 解决办法
    2008R2Win7管理一创建域和加入域
    学生表 课程表 成绩表 教师表 50个常用sql语句
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/12174032.html
Copyright © 2011-2022 走看看