本文介绍了拦截XMLHtt prequest和修改的responseText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个脚本,将作为代理/包装的原生 XMLHtt prequest 对象让我去拦截,修改的responseText并返回到原来的onreadystatechange事件。

的上下文是,如果该应用正试图接收的数据是在本地存储器中已经可用,中止 XMLHtt prequest 并通过本地存储的数据回到应用程序的成功/失败回调方法。假设我有超过现有的AJAX回调方法的应用程序没有控制权。

我原本尝试下面的想法..

  VAR发送= XMLHtt prequest.prototype.send;
XMLHtt prequest.prototype.send =功能(数据){
   //做一些东西在这里修改的responseText
   send.call(此,数据);
};
 

不过,我现在已经成立,responseText的是只读的。

然后我试着退后一步,写我自己的全面的本地代理 XMLHtt prequest ,最终结束了写我自己的版本的本地方法。类似于在这里讨论...

<一个href="http://www.ilinsky.com/articles/XMLHtt$p$pquest/#implementation-wrapping">http://www.ilinsky.com/articles/XMLHtt$p$pquest/#implementation-wrapping

但它迅速得到了混乱,而且还有修改后的数据返回回原的onreadystatechange 方法的难度。

有什么建议?这甚至可能?

解决方案

////火狐,IE8 +//VAR访问= Object.getOwnPropertyDescriptor(XMLHtt prequest.prototype,'responseText的');Object.defineProperty(XMLHtt prequest.prototype,responseText的',{得到:函数(){的console.log('得到的responseText');返回accessor.get.call(本);},设置:功能(STR){的console.log('设置的responseText:%s'的,STR);//返回accessor.set.call(这一点,STR);},配置:真});////铬,野生动物园(访问== NULL)//VAR rawOpen = XMLHtt prequest.prototype.open;XMLHtt prequest.prototype.open =功能(){如果(!this._hooked){this._hooked = TRUE;setupHook(本);}rawOpen.apply(这一点,参数);}功能setupHook(XHR){功能吸气剂(){的console.log('得到的responseText');删除xhr.responseText;VAR RET = xhr.responseText;建立();返回RET;}功能二传(STR){的console.log('设置的responseText:%s'的,STR);}功能设置(){Object.defineProperty(XHR,responseText的',{得到:吸气,设置:设置器,配置:真});}建立();}

I'm trying to build a script that will act as a proxy/wrapper for the native XMLHttpRequest object enabling me to intercept it, modify the responseText and return back to the original onreadystatechange event.

The context being, if the data the app is trying to receive is already available in local storage, to abort the XMLHttpRequest and pass the locally stored data back into the apps success/failure callback methods. Assume I have no control over the apps existing AJAX callback methods.

I had originally tried the following idea..

var send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(data){
   //Do some stuff in here to modify the responseText
   send.call(this, data);
};

But as I have now established, the responseText is read only.

I then tried taking a step back, writing my own full native proxy to XMLHttpRequest, ultimately ending up writing my own version of the native methods. Similar to what is discussed here...

http://www.ilinsky.com/articles/XMLHttpRequest/#implementation-wrapping

But it rapidly got confusing, and still have the difficulty of returning the modified data back into the original onReadyStateChange method.

Any suggestions? Is this even possible?

解决方案

//
// firefox, ie8+ 
//
var accessor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');

Object.defineProperty(XMLHttpRequest.prototype, 'responseText', {
	get: function() {
		console.log('get responseText');
		return accessor.get.call(this);
	},
	set: function(str) {
		console.log('set responseText: %s', str);
		//return accessor.set.call(this, str);
	},
	configurable: true
});


//
// chrome, safari (accessor == null)
//
var rawOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function() {
	if (!this._hooked) {
		this._hooked = true;
		setupHook(this);
	}
	rawOpen.apply(this, arguments);
}

function setupHook(xhr) {
	function getter() {
		console.log('get responseText');

		delete xhr.responseText;
		var ret = xhr.responseText;
		setup();
		return ret;
	}

	function setter(str) {
		console.log('set responseText: %s', str);
	}

	function setup() {
		Object.defineProperty(xhr, 'responseText', {
			get: getter,
			set: setter,
			configurable: true
		});
	}
	setup();
}

这篇关于拦截XMLHtt prequest和修改的responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:44