LoginSignup
33

More than 5 years have passed since last update.

jQueryのajaxでバイナリのデータを取得する

Last updated at Posted at 2017-11-01

jQueryの$.ajax()でバイナリをGETすると、parsererrorが返ってくるんだが...?

  • $.ajax()はバイナリをテキスト扱いしてしまうため
  • ググってみると、jQueryを諦めて潔くXMLHttpRequestを書いて解決してる例が多い。。
    • Henry Algusさんのブログを参照して「$.ajaxで取得する方法はある!」と紹介しつつも、「$.ajaxTransportで長い記述したくない!」とXMLHttpRequestで済ませちゃう例が多数。。
      • そもそも、このブログでは何を言っているの?

BinaryTransport

使い方

Blobで取得

$.ajax({
    url: "image.png",
    type: "GET",
    dataType: 'binary',
    responseType:'blob',
    processData: false,
    success: function(result){
    }
});

ArrayBufferで取得

$.ajax({
    url: "image.png",
    type: "GET",
    dataType: 'binary',
    responseType:'arraybuffer',
    processData: false,
    success: function(result){
    }
});

これでコードがスッキリ!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33