检索二进制数据

时间:2016-05-23 15:45:47

标签: javascript ajax

有人可以帮我这个吗?

我有一个简单的测试Java servlet,如下所示:

    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();
    }

此功能有效。它返回一个包含10个双精度数字的字节数组。我知道它一切正常,因为我可以从C#应用程序中调用它,如下所示:

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;
        }

结果字节数组的预期大小为80字节(10 * 8字节),10个数字都是预期的。

我的问题是,如何通过AJAX调用从JavaScript调用此Java servlet?

例如,我尝试了以下内容:

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);
                }

但警报框显示19(不是80,正如我希望的那样)。

感谢您的帮助。

正如所建议我尝试以下但我得到相同的结果:(

   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);
                    }

}

我仍然看到19而不是80。

2 个答案:

答案 0 :(得分:0)

您正在尝试在请求正文中发送您的参数,因为它是一个GET请求,它们应该在网址中

ajaxRequestObject.open("GET", szURL+'?'+szParams, true);

答案 1 :(得分:-1)

您应该使用此示例中的“onload”事件来获取完整的有效负载/响应。

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