使用jQuery AJAX

时间:2016-08-30 04:13:48

标签: javascript jquery ajax

我希望使用jQuery AJAX发送FormData,例如:

var uploadFormData = new FormData();
uploadFormData.append("name","value");

$.ajax({
    url : "(URL_target)",
    type : "POST",
    data : uploadFormData,
    cache : false,
    contentType : false,
    processData : false,
    success : function(r) {
        alert("Success!");
    }
});

但我也希望使用jQuery AJAX发送二进制数据,如:

var data = (...);

$.ajax({
    url: "(URL_target)",
    type: "POST",
    data : data,
    cache : false,
    contentType: "application/octet-stream",
    processData: false,
    success : function(r) {
        alert("Success!");
     }
});

如何将它们合并为一个数据并发送出去?

1 个答案:

答案 0 :(得分:7)

You can append binary data to FormData object as a Blob, File, ArrayBuffer object, or data URI

var uploadFormData = new FormData();
var data = (...);
uploadFormData.append("name","value");
uploadFormData.append("data", new Blob([data], {type:"application/octet-stream"}));

$.ajax({
  url : "(URL_target)",
  type : "POST",
  data : uploadFormData,
  cache : false,
  contentType : false,
  processData : false,
  success : function(r) {
    alert("Success!");
  }
});