简体   繁体   中英

nodejs cannot get the response data from server

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:

Client side (uses jquery): as you can see, I want the text inside '#info' change to data got from server.

$('#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.

The client side seems unable to get the data from the server side, but I cannot figure it out why.

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;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM