我正在尝试将图像从html页面发送到Django视图。但我无法获得数据。以下是我的代码。
<input type="file" id = "imageupload" onchange = "sendimg()" />
<script>
var sendimg = function(){
var img = document.getElementById("imageupload")
$.ajax({
type: "POST",
url: "/test/",
data: {
"imgfile": img,
},
processData: false,
contentType: false,
success: function(data){
console.log("success");
console.log(data);
},
failure: function(data){
console.log("failure");
console.log(data);
},
});
}
</script>
Django视图是
@csrf_exempt
def testimg(request):
print "the request",request.POST
data = {"data": "ok"}
return JsonResponse(data)
print语句给出了
the request <QueryDict: {}>
我做错了什么?