简体   繁体   English

JSON帖子未从Django视图获得响应

[英]JSON post gets no response from django view

So I tried a simple JSON post. 所以我尝试了一个简单的JSON帖子。 But my view on django side doesn't respond at all. 但是我对Django的看法完全没有回应。 JavaScript: JavaScript的:

$(function(){
var arr = { 'file': "print \"It works!\"", 'fileName' : "JSONtest", 'fileExt':".py" };

$.ajaxSetup({ 
 beforeSend: function(xhr, settings) {
     function getCookie(name) {
         var cookieValue = null;
         if (document.cookie && document.cookie != '') {
             var cookies = document.cookie.split(';');
             for (var i = 0; i < cookies.length; i++) {
                 var cookie = jQuery.trim(cookies[i]);
                 // Does this cookie string begin with the name we want?
             if (cookie.substring(0, name.length + 1) == (name + '=')) {
                 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                 break;
             }
         }
     }
     return cookieValue;
     }
     if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
         // Only send the token to relative URLs i.e. locally.
         xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
     }
 } 
});
$.ajax({
    url: '/au',
    type: 'POST',
    data: JSON.stringify(arr),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(data) {
        alert(data['link']);
    }
    });
});

view.py: view.py:

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.core.files.base import ContentFile
from django.views.decorators.csrf import ensure_csrf_cookie

from quickpad.models import FileLink

from mimetypes import types_map

from random import randint

def upload(request):
    print 'hi'
    genId = ''
    try:
        data=json.loads(request.body)
        file=data['file']
        name=data['fileName']
        ext=data['fileExt']
        newFile = FileLink(fileName=name,fileExt=ext)
        newFile.file.save(name,ContentFile(file))

        while (True):
            genId = ''.join([alphaNum[randint(0,61)] for i in xrange(8)])
            if len(FileLink.objects.filter(fileId=genId)) == 0: break
        print genId
        newFile.fileId = genId
        newFile.save()

    except:
        print 'nope'
    response = HttpResponse()
    csrf(response)
    response['link'] = genId
    return response

The print "hi" statement doesn't show nor does any new objects get created. 打印“ hi”语句不会显示,也不会创建任何新对象。 Any help would be nice. 你能帮忙的话,我会很高兴。 Thanks! 谢谢!

Sorry for the delay, the problem was the ordering in my url.py the default r'^$' was messing with another url for some reason. 很抱歉造成延迟,问题是我的url.py中的顺序由于某种原因默认r'^ $'与另一个url混淆了。 I finally caught the error after 2 hours of debugging(it was hard to debug cause there were no errors that were showing nor any debug text). 经过2个小时的调试,我终于捕获到该错误(难以调试,因为没有显示错误,也没有任何调试文本)。 Sorry for the confusion, it's my first Django app. 抱歉,这是我的第一个Django应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM