我在ajax提交的时候,提供了CSRF token参数,但是到服务器就报:Forbidden (CSRF token missing.),返回403了。
网上搜了一圈,都没找到答案,请大神帮忙看看是哪里的问题啊?
这是网页源代码
function fun_filepreajax(event, previewId, index){
var userfile = previewId.get('post_upload_file');
var formdata = new FormData();
formdata.append('name', 'post_upload_file');
formdata.append('userfile', userfile);
formdata.append('csrfmiddlewaretoken', '{{ csrf_token }}');
$.ajax({
url: '.',
type: 'POST',
// csrfmiddlewaretoken :'{{ csrf_token }}',
headers: {'X-CSRFToken': '{{ csrf_token }}'},
dataType: "json",
data:formdata,
processData: false,
contentType: false,
success: function (data) {
g_infor_win = document.getElementsByClassName('kv-upload-progress')[0];
window.setTimeout(inforWindow,2000);
},
error:function (data, status, e){
g_infor_win = document.getElementsByClassName('kv-upload-progress')[0];
window.setTimeout(inforWindow,2000);
}
});
}
这是运行时,网页的情况:
function fun_filepreajax(event, previewId, index){
var userfile = previewId.get('post_upload_file');
var formdata = new FormData();
formdata.append('name', 'post_upload_file');
formdata.append('userfile', userfile);
formdata.append('csrfmiddlewaretoken', '7xn5Ajs5mwbOUO65sjMT781tz9uT3PpbYowLIikpd0tpDGpH6vY55ynPzAacrFEX');
$.ajax({
url: '.',
type: 'POST',
// csrfmiddlewaretoken :'7xn5Ajs5mwbOUO65sjMT781tz9uT3PpbYowLIikpd0tpDGpH6vY55ynPzAacrFEX',
headers: {'X-CSRFToken': '7xn5Ajs5mwbOUO65sjMT781tz9uT3PpbYowLIikpd0tpDGpH6vY55ynPzAacrFEX'},
dataType: "json",
data:formdata,
processData: false,
contentType: false,
success: function (data) {
g_infor_win = document.getElementsByClassName('kv-upload-progress')[0];
window.setTimeout(inforWindow,2000);
},
error:function (data, status, e){
g_infor_win = document.getElementsByClassName('kv-upload-progress')[0];
window.setTimeout(inforWindow,2000);
}
});
}