简体   繁体   English

是否可以在浏览器中拦截非ajax请求?

[英]Is it possible to intercept a non-ajax request in browser?

Is it possible to intercept HTTP requests in browser with JavaScript which aren't done through XMLHttpRequest object (ajax)? 是否可以使用JavaScript拦截浏览器中的HTTP请求,这些请求不是通过XMLHttpRequest对象(ajax)完成的?

I'm mocking an API server with ember-cli-mirage , which is using pretender . 我正在使用ember-cli-mirage嘲笑一个使用pretender的API服务器。 I need to add file upload (images) to the app. 我需要将文件上传(图像)添加到应用程序。 Mocking upload is no issue since it's done by ajax. 模拟上传没有问题,因为它是由ajax完成的。 But I'm unable to intercept the GET request caused by image tag (non-ajax request) since pretender is only intercepting requests done through XMLHttpRequest and therefore can't return "uploaded" data. 但是我无法拦截由图像标记(非ajax请求)引起的GET请求,因为伪装者只是拦截通过XMLHttpRequest完成的请求,因此无法返回“上传”的数据。

Is there anyway I could also intercept non-ajax requests? 无论如何我还可以拦截非ajax请求吗?

If not: What should be considered best practice then? 如果不是:那么最佳做法应该是什么呢? This is what I've considered so far: 这是我到目前为止所考虑的:

  1. Retrieving images through ajax so I could easily intercept the request. 通过ajax检索图像,以便我可以轻松拦截请求。 Eg I could treat them as ember-data models having base64 encoded image data as an attribute. 例如,我可以将它们视为具有base64编码图像数据作为属性的余烬数据模型。 But I'm not sure about the trade-off. 但我不确定是否需要权衡利弊。 I'm afraid that this will cause performance issues and possible memory-leaks cause of image size (> 1MB). 我担心这会导致性能问题和可能的内存泄漏导致图像大小(> 1MB)。
  2. Passthrough requests for images and handling them in express server coming with ember-cli. 直通请求图像并在带有ember-cli的快速服务器中处理它们。 But then mocking file upload would only work if app is served by ember-cli. 但是,只有当应用程序由ember-cli提供时,模拟文件上传才会起作用。 And not being able to deploy development builds would be a big trade-off. 而且无法部署开发版本将是一个很大的权衡。

I think the only way to do this is to use a service worker . 我认为这样做的唯一方法是使用服务工作者

The service workers fetch event can intercept any kind of request. 服务工作者fetch事件可以拦截任何类型的请求。 However browser support is not very well yet. 但浏览器支持还不是很好。

您也许可以使用Wireshark来嗅探数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM