javascript - ajax failing getting data from pre element after it gets filled -
the thing have embedded python interpreter , after user presses "run", output interpreter gets transferred pre element. want take data pre element , send django server through ajax. problem after assigning of data variable, django gets nothing. can start interpreter , ajax script after pressing "run", both work work onclick. 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 code works perfectly
var input_string = string(document.getelementbyid("output").innerhtml); alert(input_string);
but when try use variable in ajax, server fails it. tried using async: false
, doesn't change anything. 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" , request.post.get('text') == out_compare: text = "they 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 check request.post.get('text')
empty
the question how can data ajax, variable assigned before, not sting?
it looks you're sending json server in request, variables in django you'd need do:
def lesson_validate(request,lesson_number): import json data = json.loads(request.body) text = data.get('text') # stuff.
Comments
Post a Comment