使用axios、fetchJsonp获取服务器的接口数据。其中fetchJsonp是跨域访问

一、使用axios

  1、安装axios模块

npm install --save axios

  2、引用模块

import axios from 'axios'

  3、实现请求

import axios from 'axios';

const request = (url: string, params = {}, data = {}, options) => {
// debug(url, params);return new Promise((resolve, reject) => {
axios({url, params, data, ...options})
.then((response) => {
// debug(response);
// 请求返回为json格式, 则response.data必须为对象,且必须有固定的格式,
// 这里没有处理返回值为字符串的情况, 目前没有这样的需求
reject(response);
})
.catch((error) => {
debug(error);
reject(error);
});
});
}; export function get (url: string, params?: any, options?: any) {
return request(url, params, undefined, {method: 'get', ...options});
} export function post (url: string, data?: any, options?: any) {
return request(url, undefined, data, {method: 'post', ...options});
} //这段代码还没有经过测试,如果不行可以尝试下面代码
request = (url) => {
  axios.get(url)
   .then((res) => {
    console.log(res);
   })
.catch((err) => {
  console.log(err);
})
}

二、使用fetchJsonp

  1、安装fetchJsonp模块

npm install --save fetchJsonp

  2、引用模块

import fetchJsonp from 'fetch-jsonp';

  3、使用(还没有测试的)

import fetchJsonp from 'fetch-jsonp';

jsonp (url: string, callback = null) {
return new Promise((resolve, reject) => {
fetchJsonp(url, {callback})
.then((res) => {
resolve(res);
})
.catch((err) => {
debug(err);
})
})
}

这里说下fetchJsonp的问题

在代码中应该使用script标签,引用js文件

let script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://192.168.1.100:7700/Advert/GetAdvert?elementId=adid';
document.getElementById(this.props.htmlId).appendChild(script); <div id={this.props.htmlId} style={this.props.styles}></div>
下面代码是接口http://192.168.1.100:7700/Advert/GetAdvert?elementId=adid返回的信息,其实是向代码中插入一个函数,自动运行,类似前端调用函数
(function(){
var json = {"AdvertName":"图片广告","AdvertDesc":"图片广告","AdvertHeight":"100%","AdvertId":63102,
    "AdvertImage":"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=831914849,3674285067&fm=11&gp=0.jpg",
    "AdvertWidth":"100%","JumpUrl":"http://192.168.2.88:7700/advert/JumpAdvert?advertId=63102&flowerId=1987&orderMark=7c3a8f5172494fcab09e5eee607aae66&merchantCode=",
    "AdvertWord":"图片广告"};
var elementId = '';
var div = document.createElement("div");
div.setAttribute
div.style.width = "100%";
div.style.overflow = "hidden";
var img = document.createElement("img");
img.src = json.AdvertImage;
img.style.maxWidth="100%";
img.onclick = function(){location.href=json.JumpUrl;}
div.appendChild(img); if(elementId == '')
{
var scripts = document.getElementsByTagName("script");
var script = scripts[scripts.length - 1];
var dom = script.parentNode;
dom.removeChild(script);
dom.appendChild(div);
}
else
{
var dom = document.getElementById('');
dom.appendChild(div);
} div = null;
img = null;
scripts = null;
script = null;
dom = null;
})();
 

fetchJsonp也类似这样,它使用的window[function_name] = function(){} ,可以查看它的原代码

React 获取服务器API接口数据:axios、fetchJsonp的更多相关文章

  1. 十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用

    react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数据模块实现请求数据 一.axios 获取Api数据 使用文档:https://www. ...

  2. 传递多个参数并获取Web API的数据

    近段时间学习Web Api觉得非常有意思.默认的路由情况之下,获取数据时,它不必指定Action操作名. 还有另外感想,就是自从学习asp.net MVC之后,加上jQuery,让Insus.NET已 ...

  3. 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析

    使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析 因为我在前台使用了jquery的text()方法,而不是html ...

  4. HttpClient get和HttpClient Post请求的方式获取服务器的返回数据

    1.转自:https://blog.csdn.net/alinshen/article/details/78221567?utm_source=blogxgwz4 /*  * 演示通过HttpClie ...

  5. PHP--通用化API接口数据输出 封装

    /** * 通用化API接口数据输出 * author qinpeizhou * @param $message * @param array $data * @param int $httpCode ...

  6. Html网页使用jQuery传递参数并获取Web API的数据

    昨天Insus.NET有开始学习Web API,<ASP.NET MVC的Web Api的实练>http://www.cnblogs.com/insus/p/4334316.html .其 ...

  7. 微信小程序入门教程(一)API接口数据记录

    今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来 小程序代码: httpsearch: function (name, offset, type, cb) { wx.re ...

  8. 使用js+Ajax请求API接口数据-带请求头方式

    C# http请求带请求头部分 先上代码: <script type="text/javascript"> function zLoginCheck() { var A ...

  9. C# 后台获取API接口数据

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

随机推荐

  1. 19.3 Table 1-2.S3C2440A 289-Pin FBGA Pin Assignments (Sheet 4 of 9) (Continued)

    应该为GPA22,这个在中文翻译手册里是正确的.

  2. 如何删除Kafka的Topic

    在server.properties文件中添加配置:delete.topic.enable=true 创建kafka主题: kafka-topics.sh --create --zookeeper 1 ...

  3. MQTT研究之EMQ:【JAVA代码构建X509证书【续集】】

    openssl创建私钥,获取公钥,创建证书都是比较简单的,就几个指令,很快就可以搞定,之所以说简单,是因为证书里面的基本参数配置不需要我们组装,只需要将命令行里面需要的几个参数配置进去即可.但是呢,用 ...

  4. H5外包团队 android视频压缩,使用ffmpeg方案

    android视频压缩,使用ffmpeg方案,集成fdk-aac与264编码,适用于32位系统与64位系统,支持ARM 32/64 cpu与x86 32/64 cpu,mips 32/64 cpu,即 ...

  5. Guava - Ordering

    guava中Ordering类是对Compartor接口的实现,但它也只是一个抽象类. 当调用Ordering.natural()方法时,它就会返回一个NaturalOrdering的对象,Natur ...

  6. 【C++】static关键字的总结

    转自 https://www.cnblogs.com/BeyondAnyTime/p/2542315.html C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的s ...

  7. C#工具类:Json操作帮助类(转载)

    原文转载自C#工具类:Json操作帮助类_IT技术小趣屋. Json序列化和反序列化在程序开发中时常会遇到,在C#中可以使用很多种方法实现对数据的Json序列化和反序列化,封装一个Json操作工具类来 ...

  8. ActiveMQ( 一) 同步,异步,阻塞 JMS 消息模型

    同步请求:浏览器 向服务器 发送一个登录请求,如果服务器 没有及时响应,则浏览器则会一直等待状态,直至服务器响应或者超时. 异步请求:浏览器 向服务器 发送一个登录请求,不管服务器是否立即响应,浏览器 ...

  9. C# 利用Unity 实现IOC+AOP

    public interface INoticy { void Noticy(string msg); } public class SMSNoticy : INoticy { public void ...

  10. python:面向对象初级

    面向对象编程类的概念 : 具有相同属性和技能的一类事物 人类 抽象对象 : 就是对一个类的具体的描述 具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了 ...