javascript 强制 HTTP 请求在浏览器中失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26472732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forcing an HTTP request to fail in browser
提问by Peter Maidens
Is it possible to make an http request that has been sent to a server by the browser fail without having to alter the javascript?
是否可以使浏览器发送到服务器的 http 请求失败而无需更改 javascript?
I have a POST request that my website is sending to the server and we are trying to test how our code reacts when the request fails (e.g. an HTTP 500 response). Unfortunately, the environment that I need to test it in has uglified and compressed javascript, so inserting a breakpoint or altering the javascript isn't an option. Is there a way for us to utilize any browser to simulate a failed request?
我有一个 POST 请求,我的网站正在发送到服务器,我们正在尝试测试我们的代码在请求失败时的反应(例如 HTTP 500 响应)。不幸的是,我需要测试它的环境有丑陋和压缩的 javascript,因此插入断点或更改 javascript 不是一种选择。有没有办法让我们利用任何浏览器来模拟失败的请求?
The request takes a long time to complete, so using the browser's console to run a javascript command is a possibility.
该请求需要很长时间才能完成,因此可以使用浏览器的控制台来运行 javascript 命令。
I have tried using window.stop(), however, this does not work since I need to failure code to execute.
我曾尝试使用 window.stop(),但是,这不起作用,因为我需要执行失败代码。
I am aware of the option of setting up a proxy server, but would like to avoid this is possible.
我知道设置代理服务器的选项,但希望避免这种情况是可能的。
回答by davguij
In Chrome (just checked v63), you can actually block a specific URL (or even a whole domain) from the Network tab. You only need to right-click on the entry and select Block request URL
(or Block request domain
.)
在 Chrome(刚刚检查 v63)中,您实际上可以从“网络”选项卡中阻止特定 URL(甚至整个域)。您只需要右键单击该条目并选择Block request URL
(或Block request domain
.)
回答by Macil
One possible solution is to modify the XMLHttpRequest objects that will be used by the browser. Running this code in a javascript console will cause all future AJAX calls on the page to be redirected to a different URL (which will probably give a 404 error):
一种可能的解决方案是修改浏览器将使用的 XMLHttpRequest 对象。在 javascript 控制台中运行此代码将导致页面上所有未来的 AJAX 调用都被重定向到不同的 URL(这可能会产生 404 错误):
XMLHttpRequest.prototype._old_open =
XMLHttpRequest.prototype._old_open || XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
return XMLHttpRequest.prototype._old_open.call(
this, method, 'TEST-'+url, async, user, pass);
};
回答by Macil
Don't overlook the simplest solution: disconnect your computer from the Internet, and then trigger the AJAX call.
不要忽视最简单的解决方案:断开计算机与 Internet 的连接,然后触发 AJAX 调用。
回答by Matti Virkkunen
Chrome's dev tools have an option to "beautify" (i.e. re-indent) minified JavaScript (press the "{}" button at the bottom left). This can be combined with the "XHR breakpoint" option to break when the request is made. XHR breakpoints don't support modifying the response though AFAIK, but you should be able to find a way to do it via code.
Chrome 的开发工具可以选择“美化”(即重新缩进)缩小的 JavaScript(按左下角的“{}”按钮)。这可以与“XHR 断点”选项结合使用以在发出请求时中断。XHR 断点不支持通过 AFAIK 修改响应,但您应该能够通过代码找到一种方法。
回答by theGuestFran300
Just type at the brower a changed URL, e.g. the well formed URL e.g. http://thedomain.com/welcome/by another placing "XX": http://thedomain.com/welcomeXX/, that will cause a 404 error (not found)
只需在浏览器中输入更改后的 URL,例如格式正确的 URL,例如http://thedomain.com/welcome/另一个放置“XX”:http: //thedomain.com/welcomeXX/,这将导致 404 错误(未找到)