ajax failing getting data from pre element after it gets filled
Question: The thing is that i have an embedded python interpreter and after a user
presses "Run", the output from interpreter gets transferred to a pre element.
I want to take that data from pre element and send it to django server through
AJAX. The problem is that even after assigning of that data to a variable,
django gets nothing. Also i can start interpreter and AJAX script only after
pressing "Run", both work work with onclick. I am using POST request.

    
    
    `$(document).ready(function(){
       $('#run').click(function(){
       var input_string = String(document.getElementById("output").innerHTML);
       alert(input_string);
       $.ajax({
         url: '/courses/python3/lesson_validate/{{ lesson_number }}/',
         data: {"text": input_string, csrfmiddlewaretoken: '{{ csrf_token }}'},
         dataType: "json",
         type:"POST",
         success: function(data, textStatus){
           alert('get_response');
           alert(data);
         },
         error : function(xhr,errmsg,err) {
           alert(xhr.status + ": " + xhr.responseText);
         }
       });
     });
    });
    

`

So that code works perfectly

`var input_string = String(document.getElementById("output").innerHTML);
alert(input_string); `

but when i try to use that variable in ajax, server fails to get it. I tried
using `async: false`, it doesn't change anything. This is view code:

    
    
    `def lesson_validate(request,lesson_number):
        args = {}
        args.update(csrf(request))
        out_compare = Lessons.objects.get(id=lesson_number).lesson_output
        if request.method == "POST" and request.POST.get('text') == out_compare:
            text = "they are equal"
            return HttpResponse(json.dumps(text), content_type='application/javascript')
        else:
            args['testtest']=request.POST.get('text')
            return render_to_response('course_lesson.html', args,  context_instance=RequestContext(request))`
    

After i check `request.POST.get('text')` it is empty

The question is how can i get data from ajax, from a variable assigned before,
not just from a sting?


Answer: It looks like you're sending JSON to the server in that request, so to get the
variables in Django you'd need to do:

    
    
    def lesson_validate(request,lesson_number):
      import json 
      data = json.loads(request.body)
      text = data.get('text')
      # Do stuff.
    

