我正在通过 Axios 检查资源是否存在,如果资源不存在,期望返回 404 是正常的。
但是,当返回 404 时,它会显示在控制台中。我试图捕捉错误,但这并不能阻止它被 chrome 显示。
axios.get('/api/user/checkin').then((response) => {
Vue.set(UserStore,'checkin', response.data)
this.loading = false;
}).catch((error) =>{})
Run Code Online (Sandbox Code Playgroud)
我正在检查用户是否被选中,如果他们被选中,我将返回他们签入时间的记录以及网站上的一些详细信息。
如果它们不是,那么我没有什么可返回的,所以我返回 404。我可以返回一个空白记录,但这确实让我感到很平静。
我正在尝试在Javascript中编写一个"fileExists"函数,它只是向服务器发送请求,如果发回404状态则返回false.
该功能完美无缺,但我似乎无法抑制我在谷歌Chrome控制台中收到的错误信息:GET /something/foobar 404 (Not Found)
.
有没有人知道解决这个问题的方法?
作为参考,这是我正在使用的代码:
var fileExists = function(url) {
var req = new XMLHttpRequest();
req.open('GET', url, false);
req.send();
return req.status === 200;
};
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我将 React 与 axios和 redux-promise一起使用。axios似乎没有捕获404错误,如下所示。
这是代码。
const url = FIVE_DAY_FORECAST_URL.replace("{0}", city);
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
try{
const request = axios.get(`${url}`).then(e => {
debugger; }).catch(
e => {
debugger;
return "ERROR"; // THIS FIRES, BUT DOES NOT STOP THE CONSOLE ERROR
});
debugger;
return {
type: FETCH_FIVE_DAY_FORECAST,
payload: request
};
} catch {
debugger;
console.log("Error!"); // DOES NOT HELP AS THE 404 IS NOT A JAVASCRIPT ERROR, IT'S A VALID SERVER …
Run Code Online (Sandbox Code Playgroud) 在Angular2中,抑制未找到图片的Chrome控制台错误
所以我有这段代码:
<div *ngIf="defaultImage">
<img [ngStyle]="_style.img" [ngClass]="{'img-circle': circle}" [src]="getImageUrl()" (load)="onImageLoaded()" (error)="onImageError()" />
</div>
Run Code Online (Sandbox Code Playgroud)
因此,我有此功能可以在线查找图像,如果找不到图像,我会捕获错误并采取措施。但是,我看到一个令人讨厌的控制台错误,当我将其作为烦人的原因进行开发时,我想抑制它,这可能吗?
问候
肖恩
我有一个包含动态图像的网页。如果给定路径上不存在图像,那么我想更改图像的路径。为了检查图像是否存在于给定路径上,我使用以下代码发送了 xhr 请求。
var http = new XMLHttpRequest();
var url ='http://www.logodesignlove.com/images/negative/wwf-logo-design1.jpg'; //Image URL
http.open('GET',url, false);
http.send();
return http.status!=404;
Run Code Online (Sandbox Code Playgroud)
如果路径上不存在图像,则会返回 404 错误并在控制台中显示“GET http://www.logodesignlove.com/images/negative/wwf-logo-design1.jpg 404(未找到)”。
我怎样才能防止它扔进控制台?有没有办法捕获错误?
我在服务工作者中运行了一些代码,该代码刷新了当前缓存的资源:
const RESOURCE_CACHE = 'resources-v1';
caches.open(RESOURCE_CACHE).then(function addUrlsToCache(cache) {
return cache.addAll([
'/resources/style.css',
'/resources/fonts/led',
'/resources/script.js',
'/img/roundel-tube.png',
'/img/roundel-dlr.png',
'/img/roundel-overground.png',
]);
}).catch(function (error) {
console.error("Failed to cache resources:", error.message);
})
Run Code Online (Sandbox Code Playgroud)
为了测试错误处理,我禁用了开发服务器,然后再次运行此客户端代码。在这种情况下,我希望看到一条错误消息:Failed to cache resources: Failed to fetch
。但是我实际上看到了关于这一段代码的7条错误消息:
这让我感到困惑,因为我认为他们在这里使用诺言的方式会处理其他6个,因此它们永远不会冒充控制台。我需要做些什么以使控制台永远不会显示我在代码中处理的网络错误?
javascript google-chrome-devtools es6-promise service-worker progressive-web-apps
我在玩fetch
,并且注意到当我获取的资源不存在(又名404)时,只有Chrome显示错误:
Edge和Firefox都不这样做,并且使用fetch的404错误似乎也不会触发NetworkError使我的catch错误处理程序得到通知。我应该怎么做才能避免Chrome中出现此错误?
如果您需要它,我的完整代码如下所示:
fetch("https://www.kirupa.com/book/images/learning_react2.gif", {
method: "head",
mode: "no-cors"
})
.then(function(response) {
if (response.status == 200) {
console.log("file exists!");
} else {
console.log("file doesn't exist!");
}
return response.text();
})
.catch(function(error) {
console.log("Error ", error);
});
Run Code Online (Sandbox Code Playgroud)
谢谢,基鲁帕
这是我的代码:
async [types.GET_DATA]({commit, state}, data) {
try {
const res = await axios.post('/login', {
email: data.email,
password: data.password,
});
console.log(res)
} catch(e) {
if(e.response) {
console.log(e.response)
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,每当用户发送空字段时,我都会返回 400 Bad Request。axios 所做的是将错误与错误响应一起抛出。我需要做的是删除该控制台错误消息并仅获得错误响应。
我该怎么做?
我注意到 Chrome 会记录遇到的所有 4xx 错误,fetch
而其他浏览器(至少 Safari 和 Chrome)不会记录它们。
如何防止 Chromefetch
记录到控制台 http 请求错误(例如 400 和 422)?
我打电话给我的Web API登录.该调用返回一个JSON对象,该对象具有一个名为token的属性.
在我的AuthService中,我有以下功能:
login(model: any) {
return this.http.post('http://localhost:5000/api/auth.login', model, httpOptions).pipe(map((response: Response) => {
console.log(response);
if (response) {
// localStorage.setItem('token', jwtHelper.decodeToken(response.token));
// this.decodedToken = jwtHelper.decodeToken(response.token);
// this.userToken = response.token;
}
}));
}
Run Code Online (Sandbox Code Playgroud)
以下是console.log响应的样子:
{token: "eyJhbGciOiJIUzXxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiO…CDNojrJEwmuIbLOjhCxzDwls22SA4Whpklh7zPFaR6g_1iQcQ"}
Run Code Online (Sandbox Code Playgroud)
我可以让这个函数没有返回调用它的组件,但为了在本地存储和一些其他属性中设置令牌,我使用管道,然后映射.它是否正确?我尝试访问它时,response.token会抛出一个错误,因为typescript不知道响应中的token属性.
javascript ×8
axios ×3
angular ×2
vue.js ×2
ajax ×1
console ×1
es6-promise ×1
fetch-api ×1
observable ×1
reactjs ×1
rxjs ×1