Express bodyParser cannot parse data coming from Internet Explorer

I created the server application using Node and Express 4, and the front-end using jQuery. I have an Ajax call sending some data using POST to the server:

$.ajax({
     cache: false,
     type: 'POST',
     url: Config.API_ENDPOINT_REGISTRATION,
     dataType : 'json',
     data: info,
     success: this.successHandler.bind(this)
 });

Everything works as expected in all modern browsers except IE8 and IE9.

To make it possible to call Ajax from jQuery, I had to use the XDomainRequest script provided here: https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest Before adding this script, there was no call.

Now the problem is that the request.body that I get in Express is always empty if the data comes from IE8 / IE9.

, - bodyParser, , IE8/IE9: request.body .

, .

-, ?

+4
2

, , ( ), , content-type IE8, IE9.

, , - XDomainRequest, , jQuery-ajaxTransport-XDomainRequest xdr.contentType, "" ().

, , Express.js / . :

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var registrationController = require('./controllers/registrationController');

server.use( '/registration', contentTypeOverride({
    contentType: 'application/x-www-form-urlencoded'
}));
server.use( bodyParser.json() );
server.use( bodyParser.urlencoded({
    extended: true
}));

app.post( '/registration', registrationController.index );

app.listen( 3000 );

: express-content-type-override

+2

IE9 XDomainRequest , bodyparser, json.

, , -, :

app.use(function(req, res, next) {
    // IE9 doesn't set headers for cross-domain ajax requests
    if(typeof(req.headers['content-type']) === 'undefined'){
        req.headers['content-type'] = "application/json; charset=UTF-8";
    }
    next();
})
.use(bodyParser.json());
+1

Source: https://habr.com/ru/post/1547600/


All Articles