Javascript 在Node.js中处理异常并将错误输出发送到HTML
我有一个控制台记录错误的功能,如果存在错误,我希望将相同的数据发送回HTMLJavascript 在Node.js中处理异常并将错误输出发送到HTML,javascript,jquery,node.js,ajax,express,Javascript,Jquery,Node.js,Ajax,Express,我有一个控制台记录错误的功能,如果存在错误,我希望将相同的数据发送回HTML。必须仅在出现错误时加载,并向用户显示错误消息 app.js 假设上面的代码生成了一个错误,我希望使用jQueryAjax获取HTML上的数据 index.html 现在,如果我希望在我的HMTL上显示相同的消息,我该怎么办?您可以尝试以下操作- <div id="errorReport"></div> <script type="text/javascript">
。
必须仅在出现错误时加载,并向用户显示错误消息
app.js
假设上面的代码生成了一个错误,我希望使用jQueryAjax获取HTML上的数据
index.html
现在,如果我希望在我的HMTL上显示相同的消息,我该怎么办?您可以尝试以下操作-
<div id="errorReport"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#errorReport").hide();
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
dataType:'json',
success: function(res, status) {
if(status === 500){
$("#errorReport").show();
}
console.log(res);
}
});
});
</script>
关键是
捕获服务器上的错误,并在HTTP响应中返回它。您不必使用.json({…})
,但它往往很方便
try {
cp.execSync(`docker pull mongo:'+version`);
res.status(200)
} catch (error) {
// catch the error and return it's message to the client
res.status(500).json({ error: error.message })
}
error.message
通常具有您描述的消息类型,但您也可以访问堆栈跟踪等其他字段。由于服务器返回状态码500,这将触发error
回调,因此您需要将该处理程序添加到请求中,然后将消息添加到DOM中
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
dataType:'json',
success: function(res) {
console.log(res);
},
error: function(xhr, textStatus, errorThrown) {
// parse the JSON from the server
const json = JSON.parse(xhr.responseText);
// finally, set the div's text to the error
$("#errorReport").text(json.error);
}
});
});
此外,考虑使用新的<代码> FETCH < /Case> API在香草JS中可用,而不是$.AjAccess,可以在 div/<代码>中显示Err,使用<代码> $(“yxError Read”).Text(错误发生,'+rr’);code>,在$(“#errorReport”).show()之前的一行因此,在我的ajax调用中添加错误函数在某种程度上确实有所帮助。但是,我希望在我的html
中显示控制台错误消息,现在它简单地显示带有[object][object]的错误消息。我在问题中添加了更多的细节。另外,execSync()
在回调函数中,因此从my Node.js API返回的错误很可能不适用于execSync
。您是否尝试过将execSync
包装到try catch块中?看看最新的答案。你能再帮我一个忙吗?我有一个表
,我想隐藏它,以防发生错误并执行错误函数。已尝试包括$(“#表1”).hide()代码>内部错误函数,但它似乎不是这样工作的。嗯,我希望.hide()
能够工作。我注意到您的id是tab1
,但您在注释中键入了#table1
,这可能是问题的根源?在这两种情况下,键入错误id实际上都是table1,尽管我在声明$(“#table1”).show()的地方有AJAX complete函数代码>这就是为什么即使存在错误,我仍然看到该表的原因吗?
<div id="errorReport"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#errorReport").hide();
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
dataType:'json',
success: function(res, status) {
if(status === 500){
$("#errorReport").show();
}
console.log(res);
}
});
});
</script>
try {
cp.execSync(`docker pull mongo:'+version`);
res.status(200)
} catch (error) {
//catch the error here and handle it accordingly by sending error response to client
res.status(500)
}
try {
cp.execSync(`docker pull mongo:'+version`);
res.status(200)
} catch (error) {
// catch the error and return it's message to the client
res.status(500).json({ error: error.message })
}
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
dataType:'json',
success: function(res) {
console.log(res);
},
error: function(xhr, textStatus, errorThrown) {
// parse the JSON from the server
const json = JSON.parse(xhr.responseText);
// finally, set the div's text to the error
$("#errorReport").text(json.error);
}
});
});