Django: ajax POST sending data of array of objects not working properly

navyad

I’m creating a JSON object and pushing it into an array.

var json_array = []
    var edit_info = {
                        'name': str_name,
                        'id': str_id,
                    };
    json_array.push(JSON.stringify(edit_info))

and trying to send the json_array in a POST request.

$.ajax({
        dataType: 'JSON',
        url: req_url,
        type: req_method,
        data: {'req_data': json_array},
        success: function(data){
            console.log(data.message)
        },
        error: function(){
            alert("Some error has occured.")
        }
    });

I am seeing strange behaviour from the browser (Chrome): it seems it passes two arrays as the post data:

req_data[]:{"name":"naveen","id":"11"}
req_data[]:{"name":"kavi","id":"13"}

Shouldn't it be like this?

req_data[]: [{"name":"naveen","id":"11"}', {"name":"kavi","id":"13"}]

But in views.py request.POST shows post data as it should be:

{u'req_data[]': [u'{"name":"naveen","id":"11"}', u'{"name":"kavi","id":"13"}']}

But request.POST['req_data[]']shows

{"name":"kavi","id":"13"}

I have no clue what is happening here. Anyone?

Stan Zeez

You need to use QueryDict.getlist method for request.POST and then deserialize each list item from str to dict:

import json
data = request.POST.getlist('req_data[]', [])
data = [json.loads(item) for item in data]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to use an array of objects properly

来自分类Dev

Display POST Data in Django

来自分类常见问题

AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?

来自分类Dev

ajax post working locally but not when deployed

来自分类Dev

data table search is not working on ajax call data

来自分类Dev

I can't seem to post an array of objects

来自分类Dev

AJAX Post无法使用Django成功捕获

来自分类Dev

无法在Django中运行AJAX POST

来自分类Dev

在DJANGO中的AJAX POST期间出错

来自分类Dev

Using PowerShell to import data properly into an array

来自分类Dev

PHP AJAX POST getting wrong data

来自分类Dev

why data method not working properly while getting ID?

来自分类Dev

Form sending not working anymore

来自分类Dev

Django:Ajax POST发送对象数组的数据无法正常工作

来自分类Dev

用于CSRF保护的Django ajax POST扩展beforeSend方法

来自分类Dev

$ .ajax在$ .post正常时给出错误503-Django

来自分类Dev

如何从Ajax POST解析Django View中的JSON对象

来自分类Dev

Django:带有状态视图的 POST 表单(ajax?)

来自分类Dev

如何从 Django 中的 Javascript 访问 Ajax POST 请求

来自分类Dev

Sending json data through curl/urllib2 to Pyramid application does not give a proper request.POST

来自分类Dev

How do I use ajax to post XML data to the server?

来自分类Dev

在Django视图中调用“ request.POST”时,Ajax POST不发送数据

来自分类Dev

Django-Ajax 给出错误“方法不允许(POST):/post/like/”

来自分类Dev

POST http://127.0.0.1:8000/notifications/ajax/ 403(FORBIDDEN)//使用ajax + django

来自分类Dev

POST http://127.0.0.1:8000/notifications/ajax/ 403(FORBIDDEN)//使用ajax + django

来自分类Dev

Django:即使密钥存在,将AJAX POST数据传递给Django也会产生MultiValueDictKeyError

来自分类Dev

Java Proxy - can't exchange data from HTTP GET/POST request properly

来自分类Dev

jQuery ajax post 使用 FormData() append data element 在 post 后找不到附加的数据元素

来自分类Dev

Django:将变量从get_context_data()传递到post()

Related 相关文章

  1. 1

    How to use an array of objects properly

  2. 2

    Display POST Data in Django

  3. 3

    AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?

  4. 4

    ajax post working locally but not when deployed

  5. 5

    data table search is not working on ajax call data

  6. 6

    I can't seem to post an array of objects

  7. 7

    AJAX Post无法使用Django成功捕获

  8. 8

    无法在Django中运行AJAX POST

  9. 9

    在DJANGO中的AJAX POST期间出错

  10. 10

    Using PowerShell to import data properly into an array

  11. 11

    PHP AJAX POST getting wrong data

  12. 12

    why data method not working properly while getting ID?

  13. 13

    Form sending not working anymore

  14. 14

    Django:Ajax POST发送对象数组的数据无法正常工作

  15. 15

    用于CSRF保护的Django ajax POST扩展beforeSend方法

  16. 16

    $ .ajax在$ .post正常时给出错误503-Django

  17. 17

    如何从Ajax POST解析Django View中的JSON对象

  18. 18

    Django:带有状态视图的 POST 表单(ajax?)

  19. 19

    如何从 Django 中的 Javascript 访问 Ajax POST 请求

  20. 20

    Sending json data through curl/urllib2 to Pyramid application does not give a proper request.POST

  21. 21

    How do I use ajax to post XML data to the server?

  22. 22

    在Django视图中调用“ request.POST”时,Ajax POST不发送数据

  23. 23

    Django-Ajax 给出错误“方法不允许(POST):/post/like/”

  24. 24

    POST http://127.0.0.1:8000/notifications/ajax/ 403(FORBIDDEN)//使用ajax + django

  25. 25

    POST http://127.0.0.1:8000/notifications/ajax/ 403(FORBIDDEN)//使用ajax + django

  26. 26

    Django:即使密钥存在,将AJAX POST数据传递给Django也会产生MultiValueDictKeyError

  27. 27

    Java Proxy - can't exchange data from HTTP GET/POST request properly

  28. 28

    jQuery ajax post 使用 FormData() append data element 在 post 后找不到附加的数据元素

  29. 29

    Django:将变量从get_context_data()传递到post()

热门标签

归档