[英]How to get the line number of error inside of catch on ajax call in javascript
When I use try, catch I can get line number of error by stack message.当我使用 try, catch 时,我可以通过堆栈消息获取错误的行号。
But When I use this for ajax call as below,但是当我将它用于 ajax 调用时,如下所示,
try {
await axios.get('http://example.com')
} catch (error) {
console.log(error)
}
I can't get the stack error.我无法得到堆栈错误。 It just gives me error of ajax call.
它只是给了我 ajax 调用的错误。
Isn't there any method for catching the line number in this case?在这种情况下没有任何方法可以捕获行号吗?
Thank you for reading it.感谢您阅读。
Usually, error
will contain enough information to identify the problem.通常,
error
将包含足够的信息来识别问题。 error
is an object, when you do console.log(error)
, it actually calls its toString
implementation. error
是一个 object,当你执行console.log(error)
时,它实际上调用了它的toString
实现。
To get the stack only, you can use.要仅获取堆栈,您可以使用。
try {
throw new Error('Test');
} catch (error) {
console.log(error.stack);
}
The above will only point to the JavaScript file/piece of code that fires the request to example.com
, nothing more.以上将仅指向 JavaScript 文件/触发对
example.com
的请求的代码段,仅此而已。 :) :)
If you want to see what happened with your call (more details of why it failed), and what was the response, try console.log(JSON.stringify(error))
and use Network DevTools -https://developers.google.com/web/tools/chrome-devtools/network如果您想查看您的呼叫发生了什么(有关失败原因的更多详细信息)以及响应是什么,请尝试使用
console.log(JSON.stringify(error))
并使用Network DevTools -https://developers.google。 com/web/tools/chrome-devtools/网络
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.