How to read the post request parameters using JavaScript

后端 未结 18 1406
自闭症患者
自闭症患者 2020-11-22 16:41

I am trying to read the post request parameters from my HTML. I can read the get request parameters using the following code in JavaScript.

$wnd.location.sea         


        
18条回答
  •  猫巷女王i
    2020-11-22 17:16

    It depends of what you define as JavaScript. Nowdays we actually have JS at server side programs such as NodeJS. It is exacly the same JavaScript that you code in your browser, exept as a server language. So you can do something like this: (Code by Casey Chu: https://stackoverflow.com/a/4310087/5698805)

    var qs = require('querystring');
    
    function (request, response) {
        if (request.method == 'POST') {
            var body = '';
    
            request.on('data', function (data) {
                body += data;
    
                // Too much POST data, kill the connection!
                // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
                if (body.length > 1e6)
                    request.connection.destroy();
            });
    
            request.on('end', function () {
                var post = qs.parse(body);
                // use post['blah'], etc.
            });
        }
    }
    

    And therefrom use post['key'] = newVal; etc...

提交回复
热议问题