使用AJAX请求发送图像数据

时间:2016-05-20 13:40:32

标签: python ajax

我在Python中有这个工作代码

reponse = requests.post(url, auth=auth, data=open('image.jpg','rb'))

我需要用AJAX做同样的事情,只是在这种情况下,图像是base64编码的

$.ajax({type: 'POST', 
        username: username, 
        password: password, 
        data: atob(image), 
        url: url,
        success: function(response) { ... }});

也尝试了

var fd = new FormData();
fd.append('fname', 'image.jpg');
fd.append('data', atob(image));
$.ajax({type: 'POST', 
        username: username, 
        password: password, 
        data: fd,
        processData: false, 
        contentType: false, 
        url: url,
        success: function(response) { ... }});

我似乎无法让它发挥作用。调用本身是成功的,但另一方面的API似乎没有得到图像。我错过了什么?

1 个答案:

答案 0 :(得分:0)

显然,需要手动将contentType设置为与数据对应的值。它现在正在使用正确的contentType。

相关问题