Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs无法从服务器获取响应数据_Javascript_Jquery_Node.js_Express_Http Post - Fatal编程技术网

Javascript nodejs无法从服务器获取响应数据

Javascript nodejs无法从服务器获取响应数据,javascript,jquery,node.js,express,http-post,Javascript,Jquery,Node.js,Express,Http Post,我不熟悉nodejs和jquery,我只想从服务器获取一些数据,并使用这些数据更新页面: 客户端(使用jquery):如您所见,我希望“#info”中的文本更改为从服务器获取的数据 $('#button').click(function(){ $.post("/search", function(data) { $('#info').text(data); }); }); 服务器端(使用express): 问题是:不是更新#info的内容,而是整个网页的内容都将消失,只在网页上

我不熟悉nodejs和jquery,我只想从服务器获取一些数据,并使用这些数据更新页面:

客户端(使用jquery):如您所见,我希望“#info”中的文本更改为从服务器获取的数据

$('#button').click(function(){
  $.post("/search", function(data) {
    $('#info').text(data);
  });
});
服务器端(使用express):

问题是:不是更新#info的内容,而是整个网页的内容都将消失,只在网页上打印“hello”


客户端似乎无法从服务器端获取数据,但我无法找出原因。

如我的评论中所述,您可以处理表单提交事件并停止表单提交,而不是处理按钮单击事件

$('form').submit(function(){
    $.post("/search", function(data) {
        $('#info').text(data);
    });

    // This will prevent further processing...
    return false;
});

如果你点击的是重定向,发布你点击的内容可能是个好主意,比如在
#button
元素中?@adeneo我不明白你的意思,你能解释一下吗?
#button
是表单上的提交按钮吗?@palanik它在表单中,也是提交按钮,这是个问题吗?是的。这里发生的事情是,在您点击按钮后,表单也会被提交。您可以在处理submit事件时防止这种情况。我将显示代码作为答案。我尝试使用您的方法,但问题仍然存在。客户端javascript似乎被忽略了:我注释掉客户端javascript并单击submit按钮,表单仍然被提交,并在新页面上返回数据。如果没有看到代码,很难知道发生了什么。但是,我创建它是为了说明JS代码如何阻止表单提交到服务器。嗯。
$('form').submit(function(){
    $.post("/search", function(data) {
        $('#info').text(data);
    });

    // This will prevent further processing...
    return false;
});