parsererror and Unexpected token < in JSON at position in ajax and django?

三世轮回 提交于 2019-12-14 03:05:06

问题


please look into below code

AJAX FUNCTION

<script>
$(document).ready(function() {
$("#id_module").on('change', function(){
  var mod1 = $(this).val();
  alert(mod1);
  $.ajax({
    url: 'submodule/'+ mod1,
    type:'GET',
    dataType:'json',
    success: function(response){
        alert(JSON.stringify(response));
        submod=response['submod'];
        alert(submod);
        $('#submodule').empty();
        $("#submodule").prepend($('<option>',
            {
                value: '',
                text: '-- Select Sub Module Type --'
            }));
        $.each(submod, function(ind){
        $("#submodule").append($('<option>',
            {
                value: submod[ind]['sub_module'],
                text: submod[ind]['sub_module']
            }));
            });
        $('#submodule').selectpicker("refresh");
    }
  });
});
});
</script>

My Django -- URL:

from django.urls import re_path
from django.conf import settings
from django.conf.urls.static import static
from E_Ticketing import views

urlpatterns = [re_path(r'^eForm/report$',views.reports{'template_name':'reports.html'},name='report'),re_path(r'^eForm/resolution$',views.resolutionForm{'template_name':'Resolution_Form.html'},name='resolution'),
re_path(r'^eForm/assign$',views.assignForm,{'template_name':'assign_form.html'},name='assign'),
re_path(r'^eForm',views.eticket, {'template_name':'e_ticket_form.html'},name='eticket'),
re_path(r'^eForm/submodule/(?P<n_moduleid>\d+)$',views.submodule,name='submodule'),
re_path(r'^eForm/fillemp/(?P<n_empid>\d+)$',views.fillemp,name='fillemp'),
 ]
 if settings.DEBUG:
  urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

My Django --Views:

def submodule(request,n_moduleid):
try:
    if request.method=='GET':
        submod=[]
        submod=TblTxEticketdetails.objects.using('ETicketing').values('sub_module').filter(Q(module_id=n_moduleid)).distinct()
    else:
        messages.error(request, 'Error Occurred!!!')
    data = {'submod': list(submod)}
    return JsonResponse(data, safe=False)
except Exception as e:
    messages.error(request, "Error Occured!!!")

This is my first time pasting question in stack overflow.I think i have messed up posting my question. please fell free to ask questions regarding code

i have gone through all of my code and i couldn't find where my code is wrong. while running, alert box appears but it does not go to ajax function. i need little help please!!!

i am getting error in this way

text status: parsererror
eForm:1676 error: SyntaxError:Unexpected token < in JSON at position 0

回答1:


sorry guys,

I found the answer,

in url.py I have changed

re_path(r'^eForm',views.eticket{'template_name':'e_ticket_form.html'},name='eticket'),

to

re_path(r'^eForm$',views.eticket,{'template_name':'e_ticket_form.html'},name='eticket'),

after '^eForm' have to add '$' while excetuting ajax, it is going to other views. so i am not getting the correct answer

Thanks guys for responding to my question:)



来源:https://stackoverflow.com/questions/56422147/parsererror-and-unexpected-token-in-json-at-position-in-ajax-and-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!