覆盖XMLHttpRequest的send方法 [英] Overriding XMLHttpRequest's send method

查看:162
本文介绍了覆盖XMLHttpRequest的send方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过覆盖XMLHttpRequest的send函数来记录(以及稍后修改)XMLHttpRequest发送到服务器的数据。我的函数将数据正确记录到控制台,但请求没有完成,因此浏览器会无限期地等待响应。任何想法代码有什么问题?

I'm trying to log (and later modify) the data XMLHttpRequest sends to a server by overriding XMLHttpRequest's send function. My function logs the data correctly to the console, but the request doesn't finish, so the browser keeps waiting for the response indefinitely. Any ideas what's wrong with the code?

XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;
var newSend = function(vData) { console.log("data: " + vData); realSend(vData); };
XMLHttpRequest.prototype.send = newSend;


推荐答案

XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;
// here "this" points to the XMLHttpRequest Object.
var newSend = function(vData) { console.log("data: " + vData); this.realSend(vData); };
XMLHttpRequest.prototype.send = newSend;

这篇关于覆盖XMLHttpRequest的send方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆