[英]Capture ajax request and its POST data
我想捕获一个ajax请求及其POST数据,但尚未找到任何方法。
我一直在尝试:XMLHttpRequest.prototype.open,但是还没有找到使用它捕获POST数据的任何方法。
我想这就是我想做的:
(function(open) {
XMLHttpRequest.prototype.open = function (method, url, async) {
this.addEventListener('readystatechange', function () {
if(url.indexOf("the-url-i-want-to-capture") !== -1 && this.readyState === 4 && this.status === 200) {
// do ... something ... with the POST data
}
}, false);
open.call(this, method, url, async);
};
})(XMLHttpRequest.prototype.open);
当使用本地ajax XMLHttpRequest
对象时,请勿使用原型访问open
,而是先声明变量,然后使用其提供的所有方法,例如open
, send
等。
还要注意,您不能截取和捕获数据,因为FormData对象不是可字符串化的对象(提示:图像?)。 仅当您使用GET
方法时,您才能在url
看到数据。
我添加了示例供您查看POST请求的响应
请参阅此页面,以开始使用所有详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/Guide/AJAX/Getting_Started
(function(open) { // declare new http client var req = new XMLHttpRequest(); // define what to do after req finishes req.onreadystatechange = function(){ if(req.readyState === XMLHttpRequest.DONE) console.log( req.responseText ); }; // start our POST req req.open('POST', 'https://jsonplaceholder.typicode.com/posts?test', true); // sent it req.send(); })();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.