O
O
ont0shko2015-06-19 09:15:53
JavaScript
ont0shko, 2015-06-19 09:15:53

How to pass large JSON via $.ajax and not get Request entity too large?

I am writing a small web application using Node.js + Express + jQuery.

I have a server that sends a JSON document to the client via GET. On the client, this document is edited and sent via POST back to the server.

And when the document is transferred to the server, only 1.4 kB is transferred, and the server answers me Request entity too large.

Part of the server code:

// ...

app.use(bodyParser.json());

// ...

app.post('/post_data', function(req, res) {
    console.log(req);
});

// ...


Part of client code:
$.ajax({
  url: '/post_data',
  type: 'POST',
  dataType: 'json', 
  contentType: 'application/json', 
  data: {translated:JSON.stringify(data)},
  success: function (res) {
    console.log('save');
  }
});


If you set limit for bodyParser: Then the server responds with invalid json, while only 930 bytes are transmitted. How can I send this JSON document to the server?
app.use(bodyParser.json({limits: '50mb'}));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timofey, 2015-06-19
@ont0shko

You need to add processData: false, and pass the request body as a string. bodyParser.json() treats the entire request body as a JSON document, not as form data.

ajax.json({
// ...
processData: false,
data: JSON.stringify(data)
// ...
});

Well, the problem of Request entity too large is just solved by the limits parameter, it sets the maximum size of the request body.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question