Axios JSONP Extension

1.1.8 · maintenance · verified Wed Apr 22

axios-jsonp-pro is a plugin that extends the popular Axios HTTP client, adding JSONP (JSON with Padding) request capabilities for web browsers and Node.js environments. While Axios itself is a robust, promise-based client for standard HTTP requests, it does not natively support JSONP, which is often used for cross-domain data fetching from older APIs. This package seamlessly integrates by patching the global Axios instance, allowing developers to make JSONP requests using `axios.jsonp()`. Currently at version 1.1.8, the project appears to be in maintenance mode, with its last publish date being several years ago, though it still functions as a reliable solution for JSONP needs within an Axios-powered application. Its key differentiator is providing a familiar Axios-like API for JSONP, contrasting with standalone JSONP libraries or direct `<script>` tag injection.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to perform basic and configured JSONP requests using the `axios.jsonp()` method after importing the plugin, including error handling.

import axios from 'axios';
import 'axios-jsonp-pro'; // This patches the axios instance

async function fetchUserData(userId) {
  try {
    // Basic JSONP request
    const response1 = await axios.jsonp(`/user?ID=${userId}`);
    console.log('User data (basic):', response1.data);

    // JSONP request with custom options, e.g., timeout
    const response2 = await axios.jsonp('/user', {
      timeout: 5000, // 5 seconds timeout
      params: {
        ID: userId,
        callback: 'myJsonpCallback' // Specify custom callback parameter name if needed
      }
    });
    console.log('User data (with options):', response2.data);

  } catch (error) {
    if (axios.isCancel(error)) {
      console.log('Request canceled', error.message);
    } else {
      console.error('JSONP request failed:', error);
    }
  }
}

// Example usage (replace with your actual JSONP endpoint)
fetchUserData(12345);
// Note: This code will only work if a server is configured to respond with JSONP to /user and ID parameter.

view raw JSON →