简体   繁体   English

如何检测为什么图像加载失败?

[英]How can I detect WHY an image failed to load?

I've got an img element on a page with its src being set dynamically on page load (different images for different situations, and the images are provided by clients). 我在页面上有一个img元素,其src在页面加载时动态设置(针对不同情况的不同图像,这些图像由客户端提供)。

I can detect WHEN an image fails to load and I handle that by reporting it. 我可以检测到何时无法加载图像,并通过报告来处理。 I also set a placeholder image when it fails to load. 当加载失败时,我还会设置一个占位符图像。

<img alt="Image not found" onError="failedToLoadResource" id="something" class="class1 class2">

This works great. 这很好。 However, just knowing when an image failed to load really isn't enough. 但是,仅知道图像何时加载失败还远远不够。 I need to be able to identify why it happened. 我需要确定为什么会发生。 The image is hosted on our server so 404s are unlikely, but not impossible. 该映像托管在我们的服务器上,因此不太可能出现404,但并非并非不可能。 I don't have access to the client's device and I can't reproduce the issue as described by them. 我无权访问客户端的设备,并且无法重现他们所描述的问题。

I've tried looking around but I can't find what I'm looking for... 我尝试环顾四周,但找不到我想要的东西...

I'm guessing it might be 404s, some CSP config issue, maybe some internet connection hiccup, whatever. 我猜可能是404s,一些CSP配置问题,也许是互联网连接打,,等等。 I just want to know what it is, so I can better understand it. 我只是想知道它是什么,所以我可以更好地理解它。

EDIT: Forgot to say the code is in a Cordova app, so there's no log to check. 编辑:忘记说代码在Cordova应用程序中,因此没有日志可检查。

From the client side, you can try to access image by making an ajax call then parsing the response. 在客户端,您可以尝试通过进行Ajax调用然后解析响应来访问图像。 Easily done using jQuery. 使用jQuery轻松完成。

var call = $.ajax( "image.img" )
  .fail(function( call, textStatus ) {
    alert( "Request failed: " + textStatus );
  });

From the server side, you can check the server logs, search for the correct request and see what happened. 从服务器端,您可以检查服务器日志,搜索正确的请求并查看发生了什么。 If request never came to the server it might be network/routing/firewall problem. 如果请求从未到达服务器,则可能是网络/路由/防火墙问题。

Steps to do this, however, are highly dependent on what is used in the backend. 但是,执行此步骤的步骤高度依赖于后端使用的内容。

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

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