简体   繁体   English

将大json字符串从Javascript发布到Django

[英]POST large json string from Javascript to Django

I have a small cordova (phonegap) mobile app with simple form. 我有一个简单的格式的小cordova(phonegap)移动应用程序。 I need select a file, fill other fields and save form. 我需要选择一个文件,填写其他字段并保存表格。 Then I want to send this data later. 然后我想稍后发送此数据。

How I save my form data: 我如何保存表单数据:
form fields in localStorage as json string of serialized array, and file in LocalFileSystem as reader.readAsText() in file (just file with base64 string). 形式是将LocalStorage中的表单字段作为序列化数组的json字符串,将LocalFileSystem中的文件作为file。中的reader.readAsText()进行更改(仅是具有base64字符串的文件)。

How I send data to server: 我如何将数据发送到服务器:
I push base64 string to my serialized array and make an ajax post to server stringified data. 我将base64字符串推送到序列化数组,并向服务器字符串化数据进行ajax发布。

My problem: 我的问题:
I cant send large json string (when file > 2 MB) to my server, I get an error: code 414, message Request-URI Too Long . 我无法将较大的json字符串(当文件> 2 MB时)发送到服务器,但出现错误: 代码414,消息Request-URI Too Long

How can I fix that? 我该如何解决?

You should do this like this. 您应该这样做。

$.ajax({
  type: 'POST',
  url: '<URL_HERE>', // where data should be send
  dataType: 'json',
  data: '{"a": "b"}', // your json data here
  success: function(data) {
      // This will be invoked, if server returns status code 200.
      console.log(data)
  },
});

The problem was in Django settings! 问题出在Django设置中!

DATA_UPLOAD_MAX_MEMORY_SIZE - by default it is 2.5 MB (2621440). DATA_UPLOAD_MAX_MEMORY_SIZE-默认情况下为2.5 MB(2621440)。

DATA_UPLOAD_MAX_MEMORY_SIZE = 26214400 solved my problem (25MB) DATA_UPLOAD_MAX_MEMORY_SIZE = 26214400解决了我的问题(25MB)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM