2017-10-30 22 views
0

イメージをHTMLページからDjangoビューに送信しようとしています。しかし、私はデータを得ることができません。以下は私のコードです。jQueryを使用して入力フィールドからイメージを取得する方法

<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: {}> 

私が間違って何をやっているを与えますか?

答えて

0

用してくださいあなたが入力フィールドの内容を取得するために)(.valしようとしたことがありますか? http://api.jquery.com/val/

+0

はい私は応答は同じです。 –

関連する問題