Coder Social home page Coder Social logo

xhr-overwrite's Introduction

#xhr-overwrite npm Build Status xhr-overwrite提供了一种重写XMLHttpRequest(ie ActiveXObject)的机制,可对常见的open、send等方法进行重写。大部分前端框架都有和jQuery beforeSend类似的拦截器机制,允许在请求发送前进行某些处理。xhr-overwrite则是在xhr底层做了包装,不依赖任何框架,对上层框架是透明的。

一个常见的应用场景是跨项目的前端http请求加密。不同项目使用了不同的前端框架,但请求加密的算法是一样的。此时可以通过xhr-orverwrite在xhr底层做统一的处理,否则就需要针对不同的框架编写不同的代码。

安装

####npm

npm i xhr-overwrite --save

####浏览器 直接引入xhr-overwrite.min.js。

注意:xhr-overwrite的引入一定要在其他前端框架之前,否则将不能有效拦截。

使用

xhrOverwrite({
	open: function(method, url, async) {
		// do some before open
		this.xhr.open(method, url);
	},
	send: function(data) {
		// do some before send
		this.xhr.send(data);
	}
});

xhrOverwrite(options)为一全局方法,可在options中指定需要重写的方法。重写方法的this指向xhr的包装类型的实例,this.xhr字段指向真实的xhr。

浏览器兼容性

  1. chrome
  2. fireforx
  3. safari
  4. ie6+
  5. iphone safari
  6. android browser

License

MIT:http://vinnyguitar.mit-license.org

xhr-overwrite's People

Contributors

vinnyguitar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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