资讯专栏INFORMATION COLUMN

简洁易用的基于Promise的jsonp库easy-jsonp,了解下

Dr_Noooo / 874人阅读

现在流行的axios库不支持jsonp,因此这里推荐一个jsonp库,简单易用。以下是介绍:

Easy JSONP

A minimal and lightweight JSONP implementation which is used to be a kind of cross domain solutions.

Features

Implement JSONP request from the browser

Combine URL query parameters by default behavior

Support the [Promise] API

Limit JSONP request period

Handle network error response

Install
# using npm
npm i easy-jsonp
# using yarn
yarn add easy-jsonp
# using script target
Usage

JSONP only support GET methods, same as easy-JSONP.

The code below show you how to use package as a dependency

// as a request dependency named jsonp
import jsonp from "easy-jsonp"
const jsonp = require("easy-jsonp").default
jsonp({
  url: "http://localhost",
  // global function named `callback` will be invoked when JSONP response
  callback: "callback", // any different name from request module
  timeout: 3000,
  params: {
    // eg. ?key0=0&key1=1...
    key0: 0,
    key1: 1
  }
})
  .then(res => console.log(res))
  .catch(err => console.error(err))

⚠️ Notice: Parameter callback value MUST NOT be same as request module name (eg. dependency named jsonp above code), otherwise request module only works once and function named value of parameter callback will be reset to null (internal implementation) which means the same name request module will be also reset unexpectedly.

For more customization capability, This package wouldn"t convert callback to a new name to prevent unexpected reset.
jsonp({
  // custom configuration
})

⚠️ Notice: For same reason, parameter callback value MUST NOT be jsonp.

Parameters
options parameter type required description
url String true JSONP request address
timeout Number false, default : 6000 milliseconds how long after timeout error is emitted. 0 to disable
callback String false, default : "jsonpCallback"+Date.now() global callback function name which is used to handle JSONP response.
params Object false, default: {} other parameters in query string parameters
Notice

Uncaught SyntaxError: Unexpected token :error

It mostly doesn"t support JSONP request when you are calling a JSON api. The difference between JSON api and JSONP is that JSON api response with an object like { num: 1 } (It will throw a error when client executed this response as a function. ). On the other hand, JSONP will respond with a function wrapped object like callback({ num: 1 }) and we will get what we need when client executed this response as a function.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98065.html

相关文章

  • 传统 Ajax 已死,Fetch 永生

    摘要:结果证明,对于以上浏览器,在生产环境使用是可行的。后面可以跟对象,表示等待才会继续向下执行,如果被或抛出异常则会被外面的捕获。,,都是现在和未来解决异步的标准做法,可以完美搭配使用。这也是使用标准一大好处。只允许外部传入成功或失败后的回调。 showImg(https://cloud.githubusercontent.com/assets/948896/10188666/bc9a53...

    fai1017 评论0 收藏0
  • 学习实践 - 收藏集 - 掘金

    摘要:官网地址聊天机器人插件开发实例教程一创建插件在系统技巧使你的更加专业前端掘金一个帮你提升技巧的收藏集。我会简单基于的简洁视频播放器组件前端掘金使用和实现购物车场景前端掘金本文是上篇文章的序章,一直想有机会再次实践下。 2道面试题:输入URL按回车&HTTP2 - 掘金通过几轮面试,我发现真正那种问答的技术面,写一堆项目真不如去刷技术文章作用大,因此刷了一段时间的博客和掘金,整理下曾经被...

    mikyou 评论0 收藏0
  • 分别使用 XHR、jQuery 和 Fetch 实现 AJAX

    摘要:本文详细讲述如何使用原生和来实现。使用可以无刷新地向服务端发送请求接收服务端响应,并更新页面。分别要用到的方法和方法。,,都是现在和未来解决异步的标准做法,可以完美搭配使用。这也是使用标准一大好处。 本文详细讲述如何使用原生 JS、jQuery 和 Fetch 来实现 AJAX。 AJAX 即 Asynchronous JavaScript and XML,异步的 JavaScript...

    Julylovin 评论0 收藏0
  • fetch使用常见问题及其解决办法

    首先声明一下,本文不是要讲解fetch的具体用法,不清楚的可以参考MDN fetch教程。 引言 说道fetch就不得不提XMLHttpRequest了,XHR在发送web请求时需要开发者配置相关请求信息和成功后的回调,尽管开发者只关心请求成功后的业务处理,但是也要配置其他繁琐内容,导致配置和调用比较混乱,也不符合关注分离的原则;fetch的出现正是为了解决XHR存在的这些问题。例如下面代码: f...

    pubdreamcc 评论0 收藏0
  • 跨域解决方案大全

    摘要:同源策略是处于对用户安全的考虑,如果非同源就会受到以下限制不能读取无法获得请求不能发送但是事实是经常需要借助非同源来提供数据,所以就需要进行跨域请求。 什么是跨域 注:本文完整示例地址先来说一个概念就是同源,同源指的是协议,端口,域名全部相同。 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会...

    孙淑建 评论0 收藏0

发表评论

0条评论

Dr_Noooo

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<