Git Product home page Git Product logo

jsonp's Introduction

jsonp

A simple JSONP implementation.

Installation

Install for node.js or browserify using npm:

$ npm install jsonp

Install for component(1) using component:

$ component install LearnBoost/jsonp

Install for browser using bower:

$ bower install jsonp

API

jsonp(url, opts, fn)

  • url (String) (字符串URL获取)
  • opts (Object), 可选
    • param (String) 指定回调的查询字符串参数的名称 callback (默认为回调)
    • timeout (Number) 后超时错误是如何发出长。 disable (零禁用) default (默认为60000)
    • prefix (String) 为全局JSONP回调函数响应前缀 (默认为__jp)
    • name (String) 为全局JSONP回调函数名称(默认prefix+递增的计数器)
  • fn 回调函数

The callback is called with err, data parameters.

If it times out, the err will be an Error object whose message is Timeout.

Returns a function that, when called, will cancel the in-progress jsonp request (fn won't be called).

jsonp的实现

//封装jsonp

import orginJSONP from "jsonp"

export default  function jsonp(url,data,option) {
  url+=(url.indexOf('?')<0?'?':'&')+param(data);
  return new Promise((resolve,reject)=>{
    orginJSONP(url,option,(err,data)=>{
      if(!err){
        resolve(data);
      }else {
        reject(err);
      }
    })
  })
}

function param(data) {
  let url='';
  for(var k in data){
    let value=data[k]!==undefined?data[k]:'';
    //拼接地址栏路径
    url+=`&${k}=${encodeURIComponent(value)}`;
  }
  return url?url.substring(1):'';
}

jsonp's People

Contributors

rauchg avatar tootallnate avatar rapind avatar feross avatar tj avatar juliangruber avatar yields avatar tellnes avatar voronianski avatar forbeslindesay avatar max-mapper avatar roderickhsiao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.