weixin_33737774 2016-05-23 15:45 采纳率: 0%
浏览 36

检索二进制数据

Can anyone help me with this please?

I have a simple test Java servlet as shown below:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
         byte[] bytes = ReadWaveformAsBinary();

         response.setContentType("application/octet-stream");
         response.setContentLength(bytes.length);

         ServletOutputStream servletOutputStream = response.getOutputStream();

         servletOutputStream.write(bytes, 0, bytes.length);
         servletOutputStream.flush();
         servletOutputStream.close();
    }

This function works. It returns a byte array with 10 double precision numbers in it. I know its all working because I can call it from a C# application as below:

public static bool CallWebServiceDownloadEndPoint(string szWebEndPoint, string szRequest, out double[] data)
        {
            data = null;

            bool bSuccess = true;

            WebClient webClient = new WebClient();

            try
            {
                byte[] byteData = webClient.DownloadData(szWebEndPoint + "?" + szRequest);
                Array.Reverse(byteData);

                data = CreateDoubleArrayFromByteArray(byteData);
                Array.Reverse(data);
            }
            catch
            {
                bSuccess = false;
            }

            return bSuccess;
        }

The resultant byte array has the expected size of 80 bytes (10 * 8 bytes) and the 10 numbers are all as expected.

My question is, how can I call this Java servlet from JavaScript via an AJAX call?

For instance, I tried the following:

function AJAXSendString(ajaxRequestObject, szURL, szParams, OnCallCompleted)
            {
                if (ajaxRequestObject != null)
                {
                    ajaxRequestObject.open("GET", szURL, true);

                    ajaxRequestObject.responseType = "arraybuffer";

                    ajaxRequestObject.onreadystatechange = function ()
                    {
                        if (ajaxRequestObject.readyState == 4)
                        {
                            if (ajaxRequestObject.status == 200)
                            {
                                var arrayBuffer = ajaxRequestObject.response;

                                if(arrayBuffer)
                                {
                                    var byteArray = new Uint8Array(arrayBuffer);

                                    alert(byteArray.byteLength);
                                }
                            }
                        }
                    }

                    ajaxRequestObject.send(szParams);
                }

But the alert box says 19 (not 80 as I hoped it would).

Thanks for any help.

As suggested I try the following but I get the same result :(

   function AJAXSendString2(ajaxRequestObject, szURL, szParams, OnCallCompleted)
    {
    if (ajaxRequestObject != null)
                    {
                        ajaxRequestObject.open("GET", szURL, true);

                        ajaxRequestObject.responseType = "arraybuffer";

                        ajaxRequestObject.onload = function(oEvent)
                        {
                            var arrayBuffer = ajaxRequestObject.response;

                            if(arrayBuffer)
                            {
                                var byteArray = new Uint8Array(arrayBuffer);

                                alert(byteArray.byteLength);
                            }
                        }

                        /*ajaxRequestObject.onreadystatechange = function ()
                        {
                            if (ajaxRequestObject.readyState == 4)
                            {
                                if (ajaxRequestObject.status == 200)
                                {
                                    var arrayBuffer = ajaxRequestObject.response;

                                    if(arrayBuffer)
                                    {
                                        var byteArray = new Uint8Array(arrayBuffer);

                                        alert(byteArray.byteLength);

                                        OnCallCompleted("1,-1,0,0,-1,1");
                                    }
                                }
                            }
                        }*/

                        ajaxRequestObject.send(szParams);
                    }

}

I still see 19 and not 80.

  • 写回答

2条回答 默认 最新

  • 零零乙 2016-05-23 15:51
    关注

    You should use the "onload" event as in this example to get the complete payload/response.

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

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵