vue-jsonp

A tiny library for handling JSONP request.
152,591 149
Install
npm install vue-jsonp
Latest Version:2.1.0
Vue:2
License:MIT
Last Updated:Jan 21, 2026
Links: GitHub  ·  npm
Maintainer: pwch

Vue-jsonp

npm version VueJsonp

A tiny library for handling JSONP request.

Quick Start

As Vue plugin:

import { VueJsonp } from 'vue-jsonp'

// Vue Plugin.
Vue.use(VueJsonp)

// Now you can use "$jsonp" on Vue components.
const vm = new Vue()
const data = await vm.$jsonp('/some-jsonp-url', {
  myCustomUrlParam: 'veryNice'
})

Use function directly:

import { jsonp } from 'vue-jsonp'

// Pass a response type.
const data = await jsonp<string>('/some-jsonp-url', {
  myCustomUrlParam: 'veryNice'
})

Send data and set query & function name

Send data

// The request url will be "/some-jsonp-url?type=admin&date=2020&callback=jsonp_{RANDOM_STR}".
const data = await jsonp('/some-jsonp-url', {
  type: 'admin',
  date: 2020
})

Custom query & function name

The url uniform is /url?{callbackQuery}={callbackName}&... and the default is /url?callback=jsonp_{RANDOM_STRING}&....

And you can change it like this:

// The request url will be "/some-jsonp-url?type=admin&date=2020&cb=jsonp_func".
jsonp('/some-jsonp-url', {
  callbackQuery: 'cb',
  callbackName: 'jsonp_func',
  type: 'admin',
  date: 2020
})

Module exports

  • VueJsonp: PluginObject<never>

  • jsonp<T>: (url: string, param?: IJsonpParam, timeout?: number) => Promise<T>

API

IJsonpParam

IJsonpParam is the type of param for jsonp function.

/**
 * JSONP parameter declaration.
 */
interface IJsonpParam {
  /**
   * Callback query name.
   * This param is used to define the query name of the callback function.
   *
   * @example
   * // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
   * const result = await jsonp('/some-url', {
   *   callbackQuery: 'myCallback',
   *   callbackName: 'jsonp_func',
   *   myCustomUrlParam: 'veryNice'
   * })
   *
   * @default callback
   */
  callbackQuery?: string

  /**
   * Callback function name.
   * This param is used to define the jsonp function name.
   *
   * @example
   * // The request url will be "/some-url?myCallback=jsonp_func&myCustomUrlParam=veryNice"
   * const result = await jsonp('/some-url', {
   *   callbackQuery: 'myCallback',
   *   callbackName: 'jsonp_func',
   *   myCustomUrlParam: 'veryNice'
   * })
   *
   * @default jsonp_ + randomStr()
   */
  callbackName?: string

  /**
   * Custom data.
   */
  [key: string]: any
}

Example

import Vue from 'vue'
import { VueJsonp } from 'vue-jsonp'

Vue.use(VueJsonp)

const vm = new Vue()
const { code, data, message } = await vm.$jsonp<{
  code: number,
  message: string,
  data: {
    id: number,
    nickname: string
  }
}>('/my-awesome-url', {
  name: 'MyName', age: 20
})

assert(code === 0)
assert(message === 'ok')
assert(data.id === 1)
assert(data.nickname === 'John Smith')
import { jsonp } from 'vue-jsonp'

const result = await jsonp<string>('/my-awesome-url')
assert(result === 'such a jsonp')

License

MIT

Related Packages

vue-http-rexsheng

A Vue.js plugin for providing http request

647 7
vuejs-ajax

It is a XHR plugin that works in specific features for Vue.js 2.x and and above...

1,524 7
vue

The progressive JavaScript framework for building modern web UI.

588,019,132 54,801
vue
@vueuse/shared
372,738,348 22,398
@vueuse/core

Collection of essential Vue Composition Utilities

367,188,523 22,398

Version History

Version Released License
2.1.0 MIT
2.0.0 MIT
0.1.8 MIT
0.1.7 MIT
0.1.6 MIT
Pre-releases (2)
Version Released License
2.1.0-1 MIT
2.1.0-0 MIT