POSTリクエストでフォームのフィールドからデータを取得しようとしています。私の問題は、私がブラウザを使用すると、データを含むべきオブジェクトが空であるべきですが、私がPostmanを使用している場合は機能します。Expressなしでフォームフィールドからデータを取得するにはどうすればよいですか?
formidableモジュールを使用してフォームからデータを取得しています。ここで
は私のフォームである:ここでは
<div class="form">
<form action="/signup" method="POST">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Username">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
<button type="submit" class="btn btn-default" id="signup-btn">Sign Up</button>
</form>
</div>
は、フォームのルートIです:
signupPost: function (request) {
route.post('/signup', function() {
var form = new formidable.IncomingForm();
form.parse(request, function (error, fields, files) {
if (error) {
console.error(error);
}
console.log(request.headers);
console.log(fields);
});
});
}
route.post
関数が別のモジュールで宣言されたカスタム関数です:
post: function (url, handler) {
if (this.request.method === 'POST' && this.request.url === url) {
var data = handler();
this.response.end(data);
}
}
私はpost
機能が見えるようにヘッダーを変更しようとしましたこのようなSが、それはうまくいきませんでした:
post: function (url, handler) {
if (this.request.method === 'POST' && this.request.url === url) {
this.response.writeHead(200, {
'content-type': 'multipart/form-data'
});
var data = handler();
this.response.end(data);
}
}
包みなさい、それはここで、助けは、私は、ブラウザから取り戻すヘッダです:
{ host: 'localhost:8080',
connection: 'keep-alive',
'content-length': '0',
'cache-control': 'max-age=0',
origin: 'http://localhost:8080',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
referer: 'http://localhost:8080/signup',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.8' }
そして、ここではポストマンからのものです:
{ 'cache-control': 'no-cache',
'postman-token': '72c9c23c-dc66-400e-85cb-615a633efd02',
'content-type': 'multipart/form-data; boundary=--------------------------641553275315430252355922',
'user-agent': 'PostmanRuntime/6.2.5',
accept: '*/*',
host: 'localhost:8080',
'accept-encoding': 'gzip, deflate',
'content-length': '409',
connection: 'keep-alive' }
誰でもプロジェクト全体を見たい場合は、on GitHubプロジェクトがあります。
[bodyParser](https://github.com/expressjs/body-parser)ミドルウェアでフォームデータを処理するために使用する特別な理由は何ですか?ファイルアップロードには、Formiddableをお勧めします。 – James
名前属性を持つフォーム要素のみ、フォームの送信時に値が渡されます。だからあなたはブラウザから何も投稿していない。私は恐るべきものに精通していませんが、多分それはものを変えます。 – tomtom
@James私は特にExpressを使用していません。私が知ることから、ボディパーサーはExpressを使用しています。私は今代わりに照会を使用しています。 –