Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js + jQuery,ajax send array or json to server, payload too large.

Tags:

jquery

node.js

for example: jQuery:

var sendTemp=[];
var t={
   string:'this is test string1',
   time: '2017-03-20T04:25:06.584Z',
   type:'file',
   location:'usa',
   broken:false,
}

I test the var max value array to send server, when max > 500~600 server return error message payload too large problem.

var max=600;
for(var i=0;i<max;i++){
    sendTemp.push(t);
}
$.ajax({
    async:false,
    url:"/send",
    type:"POST",
    data:{
        sendTemp:JSON.stringify(sendTemp),
    },
    success:function(data){

    },
    error:function(jqXHR, textStatus, errorThrown){

    }
});

I found a solution but did not work,in app.js

node.js:

set limit:

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

set port:

 app.post('/send',function(req, res, next){  
      console.log(req.body);
      res.end();
    });
like image 933
Finn Avatar asked Aug 03 '26 00:08

Finn


1 Answers

I faced same issue. I did ajax post and got 413 payload too large error. By checking net and documentation I solved this problem by setting limit directly to body-parser.

app.use(bodyParser({limit: '50mb'}));

I hope this will be useful.

like image 134
miradham Avatar answered Aug 05 '26 14:08

miradham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!