简体   繁体   English

Ajax API Request返回400 with base64图像数据

[英]Ajax API Request returns 400 with base64 image data

I'm trying to use the Imgur API to create a feature on my website in which when I am writing a blog post, I can drag an image into the <textarea> and it will be uploaded to Imgur and the link will be inserted into the textarea for me. 我正在尝试使用Imgur API在我的网站上创建一个功能,当我写博客文章时,我可以将图像拖到<textarea> ,然后将其上传到Imgur并将链接插入textarea对我来说。 This feature makes use of HTML5's Draggable/Droppable features, the new FileReader object, and jQuery AJAX. 此功能使用HTML5的Draggable / Droppable功能,新的FileReader对象和jQuery AJAX。

So far, when I use $.ajax() to send a request to the API, the API returns 400 errors, with the message "Filetype is not supported or the image is corrupt." 到目前为止,当我使用$.ajax()向API发送请求时,API会返回400个错误,并显示消息“文件类型不受支持或图像已损坏”。 However, I know that the filetype of my image is supported ( data/png ) and that the data being generated by the FileReader is not corrupt because I can paste the data into my URL bar and get the picture that I had inserted. 但是,我知道我的图像的文件类型是受支持的( data/png ),并且FileReader生成的数据没有损坏,因为我可以将数据粘贴到我的URL栏中并获取我插入的图片。

$(document).ready(function(e) {
  var area = document.getElementById('post_body');
  function readFiles(files) {
    console.log("readfiles called! files: ");
    console.log(files);
    var fr = new FileReader();
    fr.onload = function(event) {
      console.log(event);
      var dat = "{image:'"+event.target.result+"'}";
      console.log(dat);
      $.ajax({
        type:"POST",
        data:dat,
        url:'https://api.imgur.com/3/image',
        headers: {
          Authorization: "Client-ID CLIENT-ID-WAS-HERE"
        },
        contentType: "text/json; charset=utf-8",
        dataType: "json",
        success:function(msg) {
          console.log(msg);
          area.innerHTML += msg.data;
        },
        error:function(errormsg) {
          console.log(errormsg);
          alert("Sorry, there was an error uploading the image." + errormsg.responseText);
        }
      });
    }
    fr.readAsDataURL(files[0]);
  }

  area.ondragover = function(e) {
    e.preventDefault();
    console.log("ondragover fired!");
    $(this).addClass('hover');
  }
  area.ondragend = function(e) {
    e.preventDefault();
    console.log("ondragend fired!");
    $(this).removeClass('hover');
  }
  area.ondrop = function(e) {
    console.log("ondrop fired!!");
    if ($(this).hasClass('hover')) $(this).removeClass('hover')
    e.preventDefault();
    readFiles(e.dataTransfer.files);
  }
});

The Imgur API might not accept JSON data strings; Imgur API可能不接受JSON数据字符串; it may only accept a form-style POST data payload like 它可能只接受表单样式的POST数据有效负载

image=fQas3r...&type=base64&title=Cats%20Forever

but you're passing it a JSON string. 但是你传给它一个JSON字符串。

jQuery will auto-format your payload if you hand it an actual object for data , instead of a JSON string: 如果你把它的实际对象换成data而不是JSON字符串,jQuery会自动格式化你的有效负载:

var datObj = { image: event.target.result };
 $.ajax({
     type: "POST",
     data: datObj,
     ...

Also, since you're seding a form data string, not JSON, drop the contentType parameter from your Ajax request. 此外,由于您正在查找表单数据字符串而不是JSON,因此请从Ajax请求中删除contentType参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM