4

I have a Django application with the following front-end structure:

Jquery / input.html

<script type="text/javascript">
    $(document).ready(function(){
        var expr1 = {"name":{"key":""}};
        var expr2 = {"name":{"key":""}};
        var childFilter1 = {"filter": {"expr":expr1}};
        var childFilter2 = {"filter": {"expr":expr2}};
        var parentFilter = {"filter": {"func":"AND","leftChild":childFilter1,"rightChild":childFilter2}};
        var childFilter3 = {"filter": {"expr":""}};
        var wholeParentFilter = {"filter": {"func":"OR","leftChild":childFilter3,"rightChild":parentFilter}};

        $.ajax({
            url: "/input/",
            type: "GET",
            data: wholeParentFilter,
            dataType: "json",
            success: function(input_data){
                //alert(input_data);
            }
        });
    })
</script>

Django / views.py:

def input(request):
    data = request.GET["filter"]        
    print data

    return HttpResponse(json.dumps("ABC"))

When I try to get JSON value with request.GET["filter"], Django returns the following error and no data print out:

   data = request.GET["filter"]
File "C:\Python27\lib\site-package\django-1.6.2-py2.7.egg\django\utils\datastrucutre.py", line 301, in __getitem__
   raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'filter'"

I want to use "filter" as tree because it may include sub-filter, so I don't want to change name of "filter" in JSON, any idea for transferring data from front to end? Thanks a lot.


UPDATE

Problem is caused by wholdPatrentFilter without transferred into JSON object, add $.toJSON could solve it.

        $.ajax({
            url: "/input/",
            type: "GET",
            data: $.toJSON(wholeParentFilter),
            dataType: "json",
            success: function(input_data){
                //alert(input_data);
            }
        });

Thanks a lot @alecxe for your help.

linpingta
  • 2,324
  • 2
  • 18
  • 36
  • Let's debug it a bit. What is `request.GET` value? – alecxe May 07 '14 at 15:08
  • @alecxe, it looks like as below: "GET /input/?filter%5Bfunc%5D=OR&filter%5BleftChild%5B%5Dfilter%5D%5B......" 500 14999 , it's long string as I have many data in JSON, but I guess the front part is important so I only put them here – linpingta May 07 '14 at 15:21
  • what if you make it using `POST` method? – alecxe May 07 '14 at 15:56
  • thanks @alecxe I tried it, it returns 403 error and no data print out. Like below: "POST /input/ HTTP1.1" 403 2294 – linpingta May 07 '14 at 16:06
  • You need `csrf_token` being passed, see http://stackoverflow.com/questions/6800894/django-returns-403-error-when-sending-a-post-request and https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ – alecxe May 07 '14 at 16:08
  • @alecxe thanks, you answer me another question really~ After I add csrf_exempt, it returns the same error as I put originally, "MultiValueDictKeyError: "'filter'" and POST returns 500 error – linpingta May 07 '14 at 16:19
  • @alecxe, it's wrong because I don't transfer it into JSON in front-end, I update imformation in UPDATE part, thanks a lot guy. – linpingta May 08 '14 at 15:11

0 Answers0