Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can body-parser parse formData with Ajax? #258

Closed
sunq0001 opened this issue Aug 24, 2017 · 2 comments
Closed

can body-parser parse formData with Ajax? #258

sunq0001 opened this issue Aug 24, 2017 · 2 comments
Labels

Comments

@sunq0001
Copy link

sunq0001 commented Aug 24, 2017

i tried to use bodyParser to parse formData sent by Ajax, but req.body only returns an empty object.
does that mean bodyParser doesn't have this capability?

client.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function submitData() {
            var form = document.getElementById('form1');
            var formData = new FormData(form);
            var xhr = new XMLHttpRequest();
            xhr.open('post', 'indexFormData.html', true);
            xhr.onload = function (e) {
                if (this.status == 200) {
                    document.getElementById('result').innerHTML = this.response;
                }
            };
            xhr.send(formData);
        }
    </script>
</head>

<body>
    <form id="form1" enctype="multipart/form-data">
        firstname:
        <input type="text" name="firstname" id="txtFirstName" value="ss"></br>
        lastname:
        <input type="text" name="lastname" id="txtLastName" value="ll"></br>
        <input type="button" value="submit" onclick="submitData()" />
    </form>
    <output id="result"></output>
</body>

</html>

server.js

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.text());

app.get('/indexFormData.html', function (req, res) {
    res.sendFile(__dirname + '/indexFormData.html');
});

app.post('/indexFormData.html', function (req, res) {
    res.send(req.body);
});
app.listen(1338);

result: {}

@sunq0001
Copy link
Author

sunq0001 commented Aug 24, 2017

it is too sad, i have to use multer to solve this problem. but the code looks quite uncomfortable.

var multer = require('multer')().single();
app.use(multer);

@dougwilson
Copy link
Contributor

Hi @sunq0001 that's correct: this module does not (currently) support this feature. We have the tracking issue on this as #88 . You're always welcome to implement support as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants