【发布时间】:2020-05-24 06:31:45
【问题描述】:
我有一个功能,如果存在错误,控制台会记录错误,我希望将相同的数据发送回 HTML <div>。 <div> 只能在出现错误的情况下加载,并向用户显示错误消息。
app.js
console.log('Pulling Image from DockerHub\n');
const result1 = cp.execSync('docker pull mongo:'+version);
console.log(result1.toString());
假设上面的代码产生了一个错误,我希望使用 jQuery AJAX 在我的 HTML 中获取数据。
index.html
<div id="errorReport"></div>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://localhost:8090/api/route1",
type: 'POST',
dataType:'json',
success: function(res) {
console.log(res);
}
});
});
</script>
需要在上述子进程(app.js) 中处理错误异常,并仅在存在ERROR 时才在index.html 上渲染数据。如果cp没有返回任何错误,则无需在index.html上渲染任何数据
更新:
假设在这里const result1 = cp.execSync('docker pull mongo:'+version); 我为版本提供了不正确的值,并且子进程失败。根据execSync 语法,我无法使用回调函数来确定错误。
现在控制台确实显示了一些错误消息
Error response from daemon: manifest for mongo:a not found: manifest unknown: manifest unknown
child_process.js:660
throw err;
^
现在,如果我想在我的 HMTL <div> 上显示相同的消息,我该怎么办?
【问题讨论】:
标签: javascript jquery node.js ajax express