parsererror和意外令牌< JSON在ajax和django中的位置? [英] parsererror and Unexpected token < in JSON at position in ajax and django?

查看:171
本文介绍了parsererror和意外令牌< JSON在ajax和django中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看下面的代码

AJAX功能

<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>

我的Django-URL:

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)

我的Django-查看:

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

我已经遍历了我所有的代码,而我找不到我的代码哪里有错。在运行时,会出现警告框,但不会进入ajax功能。我需要一点帮助!!!!

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!!!

我用这种方式出错了

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


推荐答案

对不起,

我找到了答案,

在url.py中,我已更改

in url.py I have changed

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

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

在'^ eForm'之后必须加上'$'来执行ajax,这将转到其他视图。所以我没有得到正确的答案

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:)

这篇关于parsererror和意外令牌&lt; JSON在ajax和django中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆