Django GET request in AJAX -


i need html using ajax. view work fine long use jquery:

view.py

def my_ajax(request):     if request.is_ajax():         my_form = myform()         context = {             'form': my_form         }         return render(request, 'myapp/form.html', context) 

main.js (jquery load)

$(document).ready(function() {     $('#foo').click(function() {         $('#bar').load('{% url "myapp:form" %}');     }); }); 

if use javascript xmlhttprequest have remove if request.is_ajax(): view otherwise got error the view myapp.views.my_ajax didn't return httpresponse object. returned none instead.

main.js (xmlhttprequest)

(function() {     document.getelementbyid('foo').addeventlistener("click", function() {     var xhttp = new xmlhttprequest();     xhttp.onreadystatechange = function() {         if (xhttp.readystate == 4 && xhttp.status == 200) {             document.getelementbyid("bar").innerhtml = xhttp.responsetext;         }     };     xhttp.open("get", '{% url "myapp:form" %}', true);     xhttp.send();     }, false); })(); 

what i'm doing wrong in xmlhttprequest? surely missing use vanilla javascript time. thanks!

try add xhttp.setrequestheader('x-requested-with', 'xmlhttprequest'); after var xhttp = new xmlhttprequest();.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -