Open prophile opened 11 years ago
I'm using director 1.2.0, and apparently this.req.body should have parsed JSON data. However, it seems that it always contains an empty object.
Test case:
var director = require('director'); var http = require('http'); var getRoot = function() { this.res.writeHead(200, {'Content-Type': 'text/html'}); this.res.end("<!DOCTYPE html>\n" + "<html><head><title>Bees</title></head>" + "<body>" + "<script src=\"//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\"></script>" + "<script>Post = function(x) { $.ajax({'type': 'POST', 'url': 'bees', 'data': JSON.stringify(x), 'contentType': 'application/json', 'dataType': 'json'}); };</script>" + "<h1>Test</h1>" + "<script>$(function() { Post('covered in bees'); })</script>" + "</body></html>"); }; var postBees = function() { var bees = this.req.body; console.log(bees); this.res.writeHead(200, {'Content-Type': 'text/plain'}); this.res.end('Eyes.\n'); }; var router = new director.http.Router({ '': {'get': getRoot}, '/bees': {'post': postBees}}); var server = http.createServer(function(request, response) { router.dispatch(request, response, function(err) { if (err != null) { response.writeHead(404); response.end(); } }); }); server.listen(8080);
Don't JSON.stringify(x) Just pass x as an object.
JSON.stringify(x)
x
I'm using director 1.2.0, and apparently this.req.body should have parsed JSON data. However, it seems that it always contains an empty object.
Test case: