通过HTTP发送在JavaScript中的二进制数据 [英] Sending binary data in javascript over HTTP

查看:599
本文介绍了通过HTTP发送在JavaScript中的二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个HTTP POST到设备我的网络上。我想发送数据到装置的四个特定字节不幸我只似乎能够将字符串发送到设备。反正是有使用JavaScript来发送原始二进制?

下面是我用做POST的脚本,它目前没有运行,除非我把一个字符串中的数据字段。任何想法?

 (功能($){
   $阿贾克斯({
      网址:'< IP地址>将'
      键入:POST,
      的contentType:应用程序/八位字节流,

      //数据:'253,0,128,1,
      数据:0xFD008001,

      跨域:真
   });
})(jQuery的);
 

解决方案

在默认情况下,jQuery的序列化数据(传递的数据属性) - 这意味着 0xFD008001 的被传递到服务器4244668417的字符串的(10字节,而不是4),这就是为什么服务器会将其并不如预期。

这是必要的,以prevent这种行为通过设置 $。阿贾克斯属性过程数据

  

默认情况下,该数据选择通过为对象数据   (从技术上讲,任何事情不是字符串等)将被处理和   转换成一个查询串,嵌合到默认内容类型   应用程序/ x-WWW的形式urlen codeD。如果您想发送   DOM文档,或其他非处理的数据,此选项设置为false。

...但是这是整个故事的一部分: XMLHtt prequest.send 的实施有它自己的<一个href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHtt$p$pquest#Browser_Compatibility">restrictions.这就是为什么你最好的选择,我想,就是让自己的序列化器使用<一个href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">TypedArrays:

  VAR bytesToSend = [253,0,128,1]
    bytesToSendCount = bytesToSend.length;

VAR bytesArray =新Uint8Array(bytesToSendCount);
对于(VAR I = 0,1 = bytesToSendCount; I&LT;升;我++){
  bytesArray [I] = bytesToSend [I];
}

$阿贾克斯({
   网址:'%your_service_url%,
   键入:POST,
   的contentType:应用程序/八位字节流,
   数据:bytesArray,
   过程数据:假的
});
 

I'm trying to send a HTTP POST to a device on my network. I want to send four specific bytes of data to the device unfortunately I only seem to be able to send strings to the device. Is there anyway to send raw binary using javascript?

Here's the script I'm using to do the POST, it currently doesn't run unless I put a string in the data field. Any ideas?

(function ($) {
   $.ajax({
      url: '<IP of Address>',
      type: 'POST',
      contentType: 'application/octet-stream',

      //data:'253,0,128,1',
      data:0xFD008001,

      crossDomain: true
   });
})(jQuery);

解决方案

By default, jQuery serializes the data (passed in data property) - and it means 0xFD008001 number gets passed to the server as '4244668417' string (10 bytes, not 4), that's why the server treats it not as expected.

It's necessary to prevent such behaviour by setting $.ajax property processData to false:

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

... but that's only part of the whole story: XMLHttpRequest.send implementation has its own restrictions. That's why your best bet, I suppose, is to make your own serializer using TypedArrays:

var bytesToSend = [253, 0, 128, 1],
    bytesToSendCount = bytesToSend.length;

var bytesArray = new Uint8Array(bytesToSendCount);
for (var i = 0, l = bytesToSendCount; i < l; i++) {
  bytesArray[i] = bytesToSend[i];
}

$.ajax({
   url: '%your_service_url%',
   type: 'POST',
   contentType: 'application/octet-stream',  
   data: bytesArray,
   processData: false
});

这篇关于通过HTTP发送在JavaScript中的二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆