I am new to nodejs and jquery, I just want to get some data from a server and use that data to update a page:
我是nodejs和jquery的新手,我只想从服务器获取一些数据并使用该数据来更新页面:
Client side (uses jquery): as you can see, I want the text inside '#info' change to data got from server.
客户端(使用jquery):正如您所看到的,我希望'#info'中的文本更改为从服务器获取的数据。
$('#button').click(function(){
$.post("/search", function(data) {
$('#info').text(data);
});
});
Server side (uses express):
服务器端(使用快递):
app.post('/search', function(req, res){
res.send('hello');
})
The problem is: instead of updating the content of '#info', the content of the whole webpage will be gone and only 'hello' is printed on the webpage.
问题是:不是更新'#info'的内容,整个网页的内容将消失,只有'你好'打印在网页上。
The client side seems unable to get the data from the server side, but I cannot figure it out why.
客户端似乎无法从服务器端获取数据,但我无法弄明白为什么。
1 个解决方案
#1
1
As mentioned in my comments, instead of handling button click event, you could handle form submit event and stop the form from submitting.
正如我的评论中所提到的,您可以处理表单提交事件并停止提交表单,而不是处理按钮点击事件。
$('form').submit(function(){
$.post("/search", function(data) {
$('#info').text(data);
});
// This will prevent further processing...
return false;
});
#1
1
As mentioned in my comments, instead of handling button click event, you could handle form submit event and stop the form from submitting.
正如我的评论中所提到的,您可以处理表单提交事件并停止提交表单,而不是处理按钮点击事件。
$('form').submit(function(){
$.post("/search", function(data) {
$('#info').text(data);
});
// This will prevent further processing...
return false;
});