Coder Social home page Coder Social logo

xhrp's Introduction

xhrp

XMLHttpRequest of promise and export jsonp. xhrp depend on native Promise.

npm

 npm install --save xhrp

require

  var xhrp = require('xhrp');
  //or
  import * as xhrp from 'xhrp';
  //or browser
  <script src="./index.js"></script>

use

when xhrp request success ,responseText is not parse. error will return array include [err , req , settings].

ajax(options)

let promise = xhrp.ajax({
 url:'http://127.0.0.1:8888/test'
});
promise.then((result)=>{
 console.log(result);
}).catch((e)=>{
 console.log(e);
});
xhrp.abort(promise);

get(url,[options])

xhrp.get('http://127.0.0.1:8888/test')
.then((result)=>{
  console.log(result);
}).catch((e)=>{
  console.log(e);
});

post(url,[options])

xhrp.post({
    url:'http://127.0.0.1:8888/test' ,
    timeout:4000,
    headers :{Accept:'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*'}
}).then((result)=>{
    console.log(result);
}).catch((e)=>{
    console.log(e);
});

upload File:

let formdata = new FormData();
formdata.append('filename',File);
xhrp.post({
    url:'http://127.0.0.1:8888/test' ,
    timeout:4000,
    data: formdata
}).then((result)=>{
    console.log(result);
}).catch((e)=>{
    console.log(e);
});

if you want send application/json , you need set contentType and set processData=false.

jsonp(url,[options])

let promise = xhrp.jsonp({url:'http://127.0.0.1:8888/test' ,timeout:4000});
promise.then((result)=>{
  console.log(result);
}).catch((e)=>{
  console.log(e);
});
xhrp.abort(promise);

abort(promise)

xhrp.abort(promise);

settings

ajax

//请求类型
type :'GET',
//是否跨域
crossDomain: false, //ineffect of jsonp ,please set params
//超时设置  默认不超时
timeout: 0,
//数据需要被序列化,默认为true
processData: true,
//对get请求数据进行缓存
cache: true,
//url
url : 'string',
//是否异步
async:'boolean',
//用户名
user:'string',
//密码
password:'string',
//请求头数据类型
contentType:'string',
//数据
data:'object',
//设置请求头
headers:'object'

jsonp

//请求类型
type :'GET',
//是否跨域
crossDomain: false, //ineffect of jsonp ,please set params
//超时设置  默认不超时
timeout: 0,
//数据需要被序列化
processData: true,
//对get请求数据进行缓存
cache: true,
//设置script属性
params:'object',
//default server get params name is "callback" , you can set it.
//服务器回调函数参数
jsonp:'string',
//url
url : 'string'

demo

cd test
node server.js

open the test/index.html

test

cmd rum:

webpack

xhrp's People

Contributors

yi-love avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.