javascript 消除控制台中的 404 url 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12915582/
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
Eliminate 404 url error in console?
提问by user1750098
I try to eleminate an 404 error occuring because the source (src) is missing..
我尝试消除由于源 (src) 丢失而发生的 404 错误。
var $chart = $("<img />")
.addClass("trend-pic")
.error(function(){
console.log("error loading..")
});
try{
$chart.attr("src", jobs[counter].url + "test/trend")
}catch(err){
$chart.attr("src", "");
}
if tried many stuff to catch the error i.e. putting an .error(function(){})
at the end.
use the $chart.load()
- method to check if the images gets loaded?
Non of those helped?
如果尝试了很多东西来捕捉错误,即.error(function(){})
在最后放一个。使用$chart.load()
- 方法检查图像是否已加载?没有那些帮助?
GET {myURLString} 404 (Not Found)
GET {myURLString} 404 (Not Found)
Browser: Safari
浏览器:Safari
回答by alexandernst
You can't really delete those 404 errors from the console. The best you can do is make some ajax calls and see the return code, but then you'll be limited to request only to your own domain.
您无法真正从控制台中删除那些 404 错误。您能做的最好的事情是进行一些 ajax 调用并查看返回码,但是您将被限制只能请求到您自己的域。
EDIT--
编辑 -
Oh, and yes, those errors will keep showing in the "Requests" tab! They just won't appear in the "Console" tab (in Chrome).
哦,是的,这些错误会一直显示在“请求”选项卡中!它们只是不会出现在“控制台”选项卡中(在 Chrome 中)。
回答by Moszeed
I think there is no way to eliminate the error from your console, except using the right url.
In your code with the try {} catch() the error is thrown and $chart.attr
will be called again with an empty string.
我认为除了使用正确的 url 之外,没有办法消除控制台中的错误。在带有 try {} catch() 的代码中,将抛出错误$chart.attr
并将使用空字符串再次调用。
What you can do is add a check like this before you set the .attr()
您可以做的是在设置之前添加这样的检查 .attr()
if (jobs[counter].url !== void 0 &&
jobs[counter].url.length !== 0) {
$chart.attr("src", jobs[counter].url + "test/trend")
}
so you can remove the try{} catch()
所以你可以删除 try{} catch()
Hope it helps.
希望能帮助到你。
回答by Mari Selvan
Just the answer is No.Because this is the only way Server send "there is a Error".
答案是否定的。因为这是服务器发送“出现错误”的唯一方式。
Refer this one.
参考这个。
回答by Amareswar
404 is not an error. It is the way server says it does not have the source referred by the client.
404 不是错误。这是服务器说它没有客户端引用的源的方式。