1

Greeting everyone, I'm trying to pass json data from html to python, Im getting the data but the format is incorrect,

Below is my code

HTML

<select class="js-example-basic-multiple" id="Status" name="Status" multiple="multiple" style="display:inline-block;">
    {% comment %} <option value="ALL" checked="1">ALL</option> {% endcomment %}
    <option value="new">New</option>
    <option value="bid">Bid</option>
    <option value="inprogress">In Progress</option>
    <option value="onhold">On Hold</option>
    <option value="completed">Completed</option>
    <option value="archived">Archived</option>
</select>

javascript

$('#Status').change(function(){
    var selected_status = $('#Status').select2('val');
    var status_text = JSON.stringify(selected_status);
    $.ajax({
            url : '/dashboard/marketing/', // the endpoint
            type : 'GET', // http method
            data : { 'stat' : status_text,
                    'csrfmiddlewaretoken': '{{csrf_token}}'
                }, // data sent with the post request
            // handle a successful response
            success : function(data) {
            },
    });
});

view.py

stat = request.GET.getlist('stat')
    print(stat)
    for selected_status in stat:
        get_project_on_status = Project.objects.all().filter(project_stage=selected_status)
        for project in get_project_on_status:
            project_on_status_list.append(project)

The result I wanted is :

["new","bid","inprogress","onhold","completed"]

but what I'm getting is :

['["new","bid","inprogress","onhold","completed"]']

How can i remove the extra bracket? thanks!

11
  • You should pass it via post, not get. You asked your js to stringify the data, and it did. Commented Dec 4, 2017 at 8:21
  • Post your code! Commented Dec 4, 2017 at 8:22
  • Hi @Federico, thanks for the input, I'm changing it as we speak but can you show me a snippet of a proper code as well thanks. Commented Dec 4, 2017 at 8:23
  • hi @31piy i tried to send the value directly without stringify it, instead of 'stat' : status_text i used 'stat' : selected_status but now I'm not getting any value at all Commented Dec 4, 2017 at 8:29
  • Can you post the console.log for your selected_status variable? Commented Dec 4, 2017 at 8:32

1 Answer 1

2

I don't know much about python, but you need to ensure that your values aren't stringified using JSON.stringify.

var status_text = selected_status; // Remove JSON.stringify from here

$.ajax({
  url : '/dashboard/marketing/', // the endpoint
  type : 'GET', // http method
  data : { 'stat' : status_text,
           'csrfmiddlewaretoken': '{{csrf_token}}'
         }, // data sent with the post request
  // handle a successful response
  success : function(data) {
  },
...

Also, jQuery automatically handles encoding of the data. So, the actual parameters name which is submitted to the server will be stat[].

I think you will need to change the name of parameter to stat[] while retrieving the values on python side. Maybe, in this line:

stat = request.GET.getlist('stat[]')

For more details about how parameters are being sent, observe this fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/questions/23661276/… In case someone gets the same error as I do

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.