我想下载一个CSV文件,它是通过POST请求点击按钮生成的.我在casperJs和phantomJS论坛上进行了最好的研究并空手而归.在像firefox这样的普通浏览器中,在发布请求后会出现浏览器下载对话窗口.如何在PhantomJS中处理这种情况
TTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-disposition: attachment;filename=ExportData.csv
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 19 Apr 2013 23:26:40 GMT
Content-Length: 65183
Run Code Online (Sandbox Code Playgroud) 想到这些方法,每种方法的优缺点是什么?
方法1:扩充本机实例
var _XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
var xhr = new _XMLHttpRequest();
// augment/wrap/modify here
var _open = xhr.open;
xhr.open = function() {
// custom stuff
return _open.apply(this, arguments);
}
return xhr;
}
Run Code Online (Sandbox Code Playgroud)
方法2:子"类"本机XMLHttpRequest
var _XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
// definePropertys here etc
}
XMLHttpRequest.prototype = new _XMLHttpRequest());
// OR
XMLHttpRequest.prototype = Object.create(_XMLHttpRequest);
// custom wrapped methods on prototype here
XMLHttpRequest.prototype.open = function() {
// custom stuff
return _XMLHttpRequest.prototype.open.apply(this, arguments);
}
Run Code Online (Sandbox Code Playgroud)
方法3:完全代理本机XMLHttpRequest
var _XMLHttpRequest …Run Code Online (Sandbox Code Playgroud) 有没有办法拦截资源请求并直接从处理程序给出响应?像这样的东西:
page.onRequest(function(request){
request.reply({data: 123});
});
Run Code Online (Sandbox Code Playgroud)
我的用例是使用 PhantomJS 渲染一个调用我的 API 的页面。为了避免身份验证问题,我想拦截对 API 的所有 http 请求并手动返回响应,而不发出实际的 http 请求。
onResourceRequest几乎可以做到这一点,但没有任何修改功能。
我看到的可能性:
localhost,并在那里有一个服务器来侦听并响应请求。这假设在localhost.page.evaluate到页面的全局window对象。这与#1 具有相同的问题:我需要先验地知道页面需要哪些数据,并编写每个页面唯一的服务器端代码。我正在使用带有Seam的JSF的Web应用程序。我希望能够在每个ajax响应后调用JavaScript函数。我正在寻找一种无需在每个页面的每个commandLink / commandButton上放置oncomplete属性的方法。
我认为有一种方法可以设置一个servlet过滤器(拦截器?让我感到困惑),将JS调用注入每个响应中。我将对此进行调查。在此期间,如果有人有其他建议,我会非常高兴。
编辑:我认为jQuery ajaxSuccess方法可能是这里的方法,但是我不确定如何实际使用它。我什么都不能注册。我基本上想添加代码以从任何来源获取任何和所有ajax请求,以在成功时调用我的JavaScript方法。谁能告诉我正确的方法?我尝试了多种方法来做到这一点,包括将其添加jQuery("*").ajaxSuccess(function(){myFunction();});到模板xhtml文件的底部。