0

On my view, I have a textbox. Using jquery, I took the value which the user inputted. Now, I would like to send that value from my jquery file to my express module in app.js.

Thoughts?

3 Answers3

0

Ajax to your server and post some data... roughly like this:

Client js

$.ajax({
  url: '/some/path'
  type: 'POST',
  data: {text: $(textbox).val()}
}).done(function() {
  //  Do stuff after data is posted here
});

Express

app.post('/some/path', function(request, response) {
  // Do stuff with request.params.text 
});
Ryan Tuosto
  • 1,941
  • 15
  • 23
0

since you are using jQuery on the client, use jQuery to POST your data:

jquery $.post post data

since you are using Express on the server, you can use Express to retrieve POST data:

How to retrieve POST query parameters?

Dan O
  • 6,022
  • 2
  • 32
  • 50
0

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax with JQuery Demo</title>
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script>
        function getAndRenderData () {
            $.ajax({
                url: document.URL+"customers/",
                    // the URL for the request
                type: "GET",
                    // whether this is a POST or GET request
                dataType: "json",
                    // the type of data we expect back
                success: function (responseJson) {
                    // code to run if the request succeeds; parameter = response
                    var trHTML = '';
                    $.each(responseJson, function (i, customer) {
                        trHTML += '<tr><td>' + customer.firstName + '</td><td>' + customer.lastName + '</td></tr>';
                    });
                    $('#customers_table').append(trHTML);
                },
                error: function (xhr, status) {
                    // code run if request fails; raw request and status
                    console.log("Sorry, there was a problem!");
                },
                complete: function (xhr, status) {      // code to run regardless of success or failure
                    console.log("The request is complete!");
                }
            })
        }

        (function($) { $(function() {
            $('#button1').click(getAndRenderData);
        });
        })(jQuery);

    </script>
</head>
<body>
<h1>Ajax with JQuery Demo1</h1>
<p>
<button id='button1'>Click to show retrieved customer data</button>
</p>
<table id="customers_table" border='1'>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
    </tr>
</table>

</body>
</html> 

server.js

var express = require('express');
var app = express();

var customers = [
    {firstName:'Peter', lastName: 'Pan', age:13},
    {firstName:'Captain', lastName:'Hook', age:35}
];

app.get('/customers/', function(req, res) {
    res.type('text/plain');
    res.json(customers);
});

app.use('/', express.static(__dirname + '/public/'));

app.listen(3000);
Jijo Paulose
  • 1,896
  • 18
  • 20