博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简洁易用的基于Promise的jsonp库easy-jsonp,了解下
阅读量:7012 次
发布时间:2019-06-28

本文共 2411 字,大约阅读时间需要 8 分钟。

现在流行的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 npmnpm i easy-jsonp
# using yarnyarn 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 jsonpimport 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.

转载地址:http://bhqtl.baihongyu.com/

你可能感兴趣的文章
常用Maven插件介绍(全)
查看>>
Hibernate工作原理
查看>>
[翻译] 单例(Singleton)
查看>>
触发器
查看>>
两种常用小程序UI组件库
查看>>
中文名文件上传到linux服务器上以后文件名会乱码(openoffice)
查看>>
换行符
查看>>
C#语言学习--基础部分(二) 方法重载
查看>>
poj 2524 -- Ubiquitous Religions
查看>>
Linux 进程间通信
查看>>
bzoj1036
查看>>
Promise事件比timeout优先
查看>>
RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
查看>>
off文件格式(Object File Format)
查看>>
解答VS2013编译报错不准确是什么原因
查看>>
ajax的post请求
查看>>
阅读笔记《构建之法》一
查看>>
POJ 3140
查看>>
Hive on spark实践
查看>>
批量判断网页是否NOT found
查看>>