Saturday, 1 April 2017

NodeJS Express AMP form req.body undefined

NodeJS, AMP-form, TLS/SSL, Nginx all Least Version.

var bodyParser = require('body-parsre');
app.use(bodyParser.urlencoded({true}); // tried false 
app.use(bodyParser.json());

app.post('/post', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    //res.setHeader('Content-Type', 'application/env+json');
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    res.setHeader('Access-Control-Allow-Origin', '*.ampproject.org');
    //res.setHeader('Access-Control-Allow-Origin', '*.amp.cloudflare.com');
    res.setHeader('AMP-Access-Control-Allow-Source-Origin', qs.unescape('htttps://mydomain.com'));
    res.setHeader('Access-Control-Expose-Headers', 'AMP-Access-Control-Allow-Source-Origin');
    console.log('sent the name ' + req.body['name']);
    console.log('sent the name ' + req.body[0]);
    console.log('sent the name ' + req.body.name);
    console.log('sent the name ' + req.body.test); // input name test
    console.log(util.inspect(req.body));
    res.end('Yo');
});

<amp-form>
<input name="name" value="a">

amp-form no doubt https://ampbyexample.com/components/amp-form/

Failed to parse response JSON:: Unexpected token Y in JSON at position 0
log.js:458 Uncaught SyntaxError: Failed to parse response JSON:: Unexpected token Y in JSON at position 0 _reported_

token Y is res.end('Yo'); res.end('JKJK'); is token J

result

sent the name undefined
sent the name undefined
sent the name undefined
sent the name undefined
{}



via まめたろう

No comments:

Post a Comment