gpt4 book ai didi

javascript - Django: SyntaxError: 意外的标记 <

转载 作者:行者123 更新时间:2023-11-29 19:45:50 24 4
gpt4 key购买 nike

我使用带有 jquery 的 django 来使用 ajax 上传文件。但是,在文件上传后(并且文件确实上传正确;数据库已更新并且它们在服务器上)javascript 返回 SyntaxError: Unexpected token <错误。

不知道这是javascript的错误还是django的错误;我看到的其他问题似乎建议确保信息确实采用 JSON 格式,但在我的情况下似乎格式正确。我真的不明白为什么会出现错误,因为一切似乎都正常。我的日志中没有错误。

如果我能看到更有用的错误消息,我就能更轻松地解决问题。

这是我的views.py:

def response_mimetype(request):
if "application/json" in request.META['HTTP_ACCEPT']:
return "application/json"
else:
return "text/plain"

class JSONResponse(HttpResponse):
"""JSON response class."""

def __init__(self,obj='',json_opts={},mimetype="application/json",*args,**kwargs):
content = simplejson.dumps(obj,**json_opts)
a = super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs)

class UploadedFileCreateView(CreateView):
model = UploadedFile
form_class = UploadedFileForm

def form_valid(self, form):
self.object = form.save(commit=False)
self.object.project_id = self.kwargs['proj_key']
self.object.save()
f = self.request.FILES.get('file')

data = [{
'name': self.object.name(),
'url': "/uploads/xmlfiles/" + self.object.name().replace(" ", "_"),
'delete_url': reverse('fileupload:upload-delete',
kwargs={'pk':self.object.id,
'proj_key':self.kwargs['proj_key']}),
'delete_type': "DELETE"}]

response = JSONResponse(data, {}, response_mimetype(self.request))

response['Content-Disposition'] = 'inline; filename=files.json'

return super(UploadedFileCreateView, self).form_valid(form)

def get_context_data(self, **kwargs):
context = super(UploadedFileCreateView, self).get_context_data(**kwargs)
context['files'] = UploadedFile.objects.all()
context['proj'] = int(self.kwargs["proj_key"])
return context

这是我的 models.py:

class UploadedFile(models.Model):
"""This represents a file that has been uploaded to the server."""
STATE_UPLOADED = 0
STATE_ANNOTATED = 1
STATE_PROCESSING = 2
STATE_PROCESSED = 4
STATES = (
(STATE_UPLOADED, "Uploaded"),
(STATE_ANNOTATED, "Annotated"),
(STATE_PROCESSING, "Processing"),
(STATE_PROCESSED, "Processed"),
)

status = models.SmallIntegerField(choices=STATES,
default=0, blank=True, null=True)
file = models.FileField(upload_to=settings.XML_ROOT)
project = models.ForeignKey(Project)

def __unicode__(self):
return self.file.name

def name(self):
return os.path.basename(self.file.name)

def save(self, *args, **kwargs):
if not self.status:
self.status = self.STATE_UPLOADED
super(UploadedFile, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
os.remove(self.file.path)
self.file.delete(False)
super(UploadedFile, self).delete(*args, **kwargs)

def get_absolute_url(self):
return u'/upload/projects/%d' % self.id

class UploadedFileForm(ModelForm):
class Meta:
model = UploadedFile
exclude = ('project',)

最佳答案

根据文档hereform_valid() 方法需要将 JSON 作为 HTTPResponse 返回。在父类(super class)上调用 form_valid() 将覆盖前两行创建的响应。

编辑 - 这对我有用:

def response_mimetype(request):
if "application/json" in request.META['HTTP_ACCEPT']:
return "application/json"
else:
return "text/plain"

class UploadedFileCreateView(CreateView):
model = UploadedFile
form_class = UploadedFileForm

def form_valid(self, form):
self.object = form.save(commit=False)
self.object.project_id = self.kwargs['proj_key']
self.object.save()
f = self.request.FILES.get('file')

data = [{
'name': self.object.name(),
'url': "/uploads/xmlfiles/" + self.object.name().replace(" ", "_"),
'type': "application/xml",
'size': self.object.file.size,
'delete_url': reverse('fileupload:upload-delete',
kwargs={'pk':self.object.id,
'proj_key':self.kwargs['proj_key']}),
'delete_type': "DELETE"}]

return HttpResponse(simplejson.dumps(data), content_type = response_mimetype(self.request))

上面链接中的代码将 ajax 响应代码捆绑为 Mixin,这将允许项目中的其他表单重用该代码。

在 Firefox 插件 Firebug、Net 选项卡或 Chrome 等效项中查看对 Ajax 调用的响应会有所帮助。这些将显示请求和响应 header ,确保发送和返回正确的设置。

关于javascript - Django: SyntaxError: 意外的标记 <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19780287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com