weixin_33709219 2018-08-25 21:21 采纳率: 0%
浏览 23

NODE检索表单变量

I am French and no one in English so everything is translated by google! I am a beginner on NODE.

In my index.html file, there is a form that I send to the server via $ .ajax with the POST method

To retrieve this form on the server

app.post('/connexion', function (req, res) {
 
    var postData = '';
    var pathname = url.parse(req.url).pathname;
        console.log('Reçue: '+ pathname +' .');
        req.setEncoding('utf8');
        req.addListener('data', function(postDataChunk) {
            postData += postDataChunk;
            console.log('POST reçu ' + postDataChunk +'.');
        });

and on the console, I receive this in return

Reçue: /connexion .
POST reçu -----------------------------4511735334446617171533446239
Content-Disposition: form-data; name="pseudo"
 
Nath
-----------------------------4511735334446617171533446239
Content-Disposition: form-data; name="mdp"
 
moi
-----------------------------4511735334446617171533446239--
.
It's already good, I see that the form is sent!

But I do not find how to retrieve the values of the variables pseudo and mdp.

Can you help me understand and recover these values?

Thank you for your help

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33691817 2018-08-26 01:14
    关注

    "body-parser" works very well to recover variables sent "normally" from a form but I can not retrieve these variables if I send them in post method via $ .ajax Maybe I'm doing my job wrong. On my server file

    app.post('/', function(req, res) {
    var pseudo = req.body.pseudo;
    var mdp = req.body.MdP;
    
    console.log(pseudo);
    console.log(mdp);
    
    res.send(true);
    

    });

    in the client.js

    $('#connect_Chat').on('submit', function (event) {
    event.preventDefault();
    //on récupère le pseudo et le mot de passe
    var pseudo =  $('#login').val().trim();
    var mdp = $('#MdP').val();
    var formDataConnectUser = new FormData();
    formDataConnectUser.append('pseudo', pseudo);
    formDataConnectUser.append('MdP', mdp);
    $.ajax({
        url: 'https://localhost:8080/',
        type: 'post',
        data: formDataConnectUser,
        processData: false,
        contentType: false,
        success: function(data) {
            if (data === true) {
                $('body').removeAttr('id');
                $('#text-plus-4').modal('hide');
                $('#boutonConnexion').html('<div class="form-group" style="float:right;"><label for="n">Votre pseudo :</label><input type="text" class="pseudoNew" value="'+user.username+'" disabled="disabled"></div>');
                $('#envoi_message').removeAttr('disabled');
                $('#message').removeAttr('disabled');
                $('#message').focus();
            } else {
                alert('Mauvais pseudo ou Mot de Passe !');
            }
        }
    });
    

    });

    In the server console, variables are displayed as undefined.

    评论

报告相同问题?