ajax post django,Django中的Ajax POST请求失败

我正在制作一个Ajax POST请求,但在我的视图中无法识别它。

views.py中的代码:

@csrf_exempt

def upload(request):

if request.method == 'POST':

form = UploadFileForm(request.POST, request.FILES)

if form.is_valid():

#handle_uploaded_file(request.FILES['file'])

f = request.FILES['file']

global globalVarForToTrackUpload

global globalFileSizeVariable

globalFileSizeVariable = f.size

filename = "/static/Data/" + f.name

destination = open(filename, 'wb+')

for chunk in f.chunks():

destination.write(chunk)

globalVarForToTrackUpload += len(chunk)

destination.close()

#return render_to_response('uploadsuccess.html')

allValues = str(globalVarForToTrackUpload) + " : " + str(globalFileSizeVariable)

return HttpResponse(allValues)

else:

form = UploadFileForm()

return render_to_response('upload.html', {'form': form})

我的中间件设置是:

MIDDLEWARE_CLASSES = (

'django.middleware.common.CommonMiddleware',

'django.contrib.sessions.middleware.SessionMiddleware',

'django.middleware.csrf.CsrfViewMiddleware',

'django.contrib.auth.middleware.AuthenticationMiddleware',

'django.contrib.messages.middleware.MessageMiddleware',

'django.middleware.csrf.CsrfResponseMiddleware',

)

我的javascript函数是:

function submitForm()

{

//document.forms["myForm"].submit();

xhrPost = getXhrObject();

var arrFiles = document.getElementById('id_file');

var fileToUpload = arrFiles.files[0];

xhrPost.onreadystatechange = function() {

if(xhrPost.readyState == 4 && xhrPost.status == 200)

document.getElementById("upload-progress-bar").innerHTML = xhrPost.responseText;

else

document.getElementById("upload-progress-bar").innerHTML = "processing upload...";

}

xhrPost.open("POST","/upload.psp/",true);

var boundary = "AJAX--------------" + (new Date).getTime();

var contentType = "multipart/form-data; boundary=" + boundary;

xhrPost.setRequestHeader("Content-Type", contentType);

xhrPost.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));

xhrPost.send(fileToUpload);

return false;

}

谁能告诉我我失踪了什么?为什么请求没有在views.py中的“upload”函数中被识别为“POST”?

提前致谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值