通过HTTP在javascript中发送二进制数据

时间:2022-08-26 19:01:30

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?

我正在尝试将HTTP POST发送到网络上的设备。我想向设备发送四个特定字节的数据,遗憾的是我似乎只能将字符串发送到设备。无论如何使用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?

这是我用来执行POST的脚本,除非我在数据字段中放入一个字符串,否则它当前不会运行。有任何想法吗?

(function ($) {   $.ajax({      url: '<IP of Address>',      type: 'POST',      contentType: 'application/octet-stream',      //data:'253,0,128,1',      data:0xFD008001,      crossDomain: true   });})(jQuery);

3 个解决方案

#1


44  

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.

默认情况下,jQuery序列化数据(在数据属性中传递) - 这意味着0xFD008001号码作为'4244668417'字符串(10字节,而不是4)传递给服务器,这就是服务器不按预期处理它的原因。

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

通过将$ .ajax属性processData设置为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.

默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x-www-form-urlencoded” 。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为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:

...但这只是整个故事的一部分:XMLHttpRequest.send实现有自己的限制。这就是为什么我最好的选择是使用TypedArrays创建自己的序列化器:

// Since we deal with Firefox and Chrome only var bytesToSend = [253, 0, 128, 1],    bytesArray = new Uint8Array(bytesToSend);$.ajax({   url: '%your_service_url%',   type: 'POST',   contentType: 'application/octet-stream',     data: bytesArray,   processData: false});

Or without using jQuery at all:

或者根本不使用jQuery:

var bytesToSend = [253, 0, 128, 1],    bytesArray = new Uint8Array(bytesToSend);var xhr = new XMLHttpRequest();xhr.open('POST', '%your_service_url%');xhr.setRequestHeader('Content-Type', 'application/octet-stream');xhr.send(bytesArray);

#2


6  

You can send binary data via ajax with xhr2, you can send the data as a typed array or a blob.

您可以通过ajax使用xhr2发送二进制数据,您可以将数据作为类型化数组或blob发送。

(function ($) {   var data = new Uint32Array(1);   data[0] = 0xFD008001;    $.ajax({      url: '<IP of Address>',      type: 'POST',      contentType: false,      processData: false,      //data:'253,0,128,1',      data:data,      crossDomain: true   });})(jQuery);

https://developer.mozilla.org/en-US/docs/Web/API/Uint32Array

#3


0  

You could use atob() and btoa():

你可以使用atob()和btoa():

var data = new Uint32Array(1);data[0] = 0xFD008001;atob(data)

This converts your binary data into a base64 string that can be sent as text.

这会将二进制数据转换为可以作为文本发送的base64字符串。

#1


44  

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.

默认情况下,jQuery序列化数据(在数据属性中传递) - 这意味着0xFD008001号码作为'4244668417'字符串(10字节,而不是4)传递给服务器,这就是服务器不按预期处理它的原因。

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

通过将$ .ajax属性processData设置为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.

默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x-www-form-urlencoded” 。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为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:

...但这只是整个故事的一部分:XMLHttpRequest.send实现有自己的限制。这就是为什么我最好的选择是使用TypedArrays创建自己的序列化器:

// Since we deal with Firefox and Chrome only var bytesToSend = [253, 0, 128, 1],    bytesArray = new Uint8Array(bytesToSend);$.ajax({   url: '%your_service_url%',   type: 'POST',   contentType: 'application/octet-stream',     data: bytesArray,   processData: false});

Or without using jQuery at all:

或者根本不使用jQuery:

var bytesToSend = [253, 0, 128, 1],    bytesArray = new Uint8Array(bytesToSend);var xhr = new XMLHttpRequest();xhr.open('POST', '%your_service_url%');xhr.setRequestHeader('Content-Type', 'application/octet-stream');xhr.send(bytesArray);

#2


6  

You can send binary data via ajax with xhr2, you can send the data as a typed array or a blob.

您可以通过ajax使用xhr2发送二进制数据,您可以将数据作为类型化数组或blob发送。

(function ($) {   var data = new Uint32Array(1);   data[0] = 0xFD008001;    $.ajax({      url: '<IP of Address>',      type: 'POST',      contentType: false,      processData: false,      //data:'253,0,128,1',      data:data,      crossDomain: true   });})(jQuery);

https://developer.mozilla.org/en-US/docs/Web/API/Uint32Array

#3


0  

You could use atob() and btoa():

你可以使用atob()和btoa():

var data = new Uint32Array(1);data[0] = 0xFD008001;atob(data)

This converts your binary data into a base64 string that can be sent as text.

这会将二进制数据转换为可以作为文本发送的base64字符串。