【发布时间】:2019-11-26 19:06:45
【问题描述】:
我有一个获取请求:
fetch(url, {
}.then(function(resp))
// do something
}.catch(err => {
console.log(err);
}
我的问题涉及获取err 的整个堆栈跟踪。例如,这里是真正的错误信息:
Access to fetch at 'https://www.google.com/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
太好了。这是一个有用的消息。但是,console.log(err) 会打印出TypeError: Failed to fetch。这是一个非常无用的错误消息。有什么方法可以捕获记录在控制台中的整个消息(即var message = //the full error?err.stack 似乎不是一个函数,err.message 返回同样无用的Failed to fetch 消息
【问题讨论】:
-
在
.then()中抛出一个console.log(JSON.stringify(resp) );,看看它会返回什么? -
@Snowmonkey 没有出现
-
你在 repl.it、codepen 或 jsfiddle 上有这个吗?
-
@Snowmonkey 给你jsfiddle.net/a490qkvd
标签: javascript fetch