i just want to pass the selected id of the option via ajax request to django 2.1 but it always returns me some errors . i am new to django and web development so please help to solve this issue js code
document.addEventListener('DOMContentLoaded',()=>{
document.querySelector('#mainoption').onchange=()=>{
const id=document.querySelector('#mainoption').value;
const request=new XMLHttpRequest;
request.open('GET','submain');
request.send(id);
alert("selected "+id);
}
});
django code
def submain(request):
subid = request.GET.get('id')
print(subid)
return HttpResponse(subid)
the out put of subid is none why this happen
The error message tells you that the submain view is not returning an HttpResponse object. You need to return an HttpResponse object.
from django.http import HttpResponse
def submain(request):
subid = request.POST.get('id')
return HttpResponse(subid)
Change your AJAX request to
document.addEventListener('DOMContentLoaded',()=>{
document.querySelector('#mainoption').onchange=()=>{
const id=document.querySelector('#mainoption').value;
const request=new XMLHttpRequest;
request.open('POST','submain');
request.send("id="+id);
alert("selected "+id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.