Node.js 当没有来自服务器节点的响应时,如何处理?
我是node.js初学者 当我已经检查我的服务器打开,但 当我尝试打开index.html时,没有任何反应 我让目录什么都没反应为什么会这样? DBtestApp>ajax\u test>www>index.html 我将nodejs设置保存在DBtestApp中 但当我进入3000端口时 给我看看“快车” 欢迎来到Express' 如何查看我的index.html 我在访问时已检查服务器是否打开 页面向我显示“Express welcome to Express”,但我不明白index.html为什么没有打开 这是我的index.htmlNode.js 当没有来自服务器节点的响应时,如何处理?,node.js,ajax,Node.js,Ajax,我是node.js初学者 当我已经检查我的服务器打开,但 当我尝试打开index.html时,没有任何反应 我让目录什么都没反应为什么会这样? DBtestApp>ajax\u test>www>index.html 我将nodejs设置保存在DBtestApp中 但当我进入3000端口时 给我看看“快车” 欢迎来到Express' 如何查看我的index.html 我在访问时已检查服务器是否打开 页面向我显示“Express welcome to Express”,但我不明白index.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#ButtonRequest').click(function(){
$.ajax({
url: 'http://localhost:3000/Echo',
data:{
myData: "Hello",
},
dataType: 'jsonp',
success: function(data){
console.log(data);
$('.Response').text('answer from server : '+data.result);
},
error: function(){
$('.Response').text('error occured');
}
});
});
});
</script>
</head>
<body>
<div class="Response"></div>
<input type="button" id="ButtonRequest" value="AJAX start" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>
当我在终端上写的时候
node ajax_srv_test
没有反应启动服务器的代码在
/Echo
路由内,这就是它从未执行导致HTTP服务器无法启动的原因。请将/Echo
路由的代码更改为以下内容:
// Echo service
app.get('/Echo', function(req, res){
var callbackName = req.query.callback;
var myData = req.query.myData;
var ResObj = {};
ResObj.result = myData;
res.header('Content-type','application/json');
res.header('Charset','utf8');
res.send(callbackName + '('+JSON.stringify(ResObj)+');');
});
var server = app.listen(3000, function(){
console.log("Express server has started on port 3000");
});
啊,对不起!我也很感激你,只是我记下了你的答案,非常感谢
// Echo service
app.get('/Echo', function(req, res){
var callbackName = req.query.callback;
var myData = req.query.myData;
var ResObj = {};
ResObj.result = myData;
res.header('Content-type','application/json');
res.header('Charset','utf8');
res.send(callbackName + '('+JSON.stringify(ResObj)+');');
});
var server = app.listen(3000, function(){
console.log("Express server has started on port 3000");
});