简体   繁体   中英

How to fix “net::ERR_EMPTY_RESPONSE” error from Jquery Ajax Post Request?

The Problem I am sending a HSV color using a Jquery ajax post request. The HSV color (Hue, Saturation, and Value) are all integers 0-255. So for the H,S,and V together I am sending 3 bytes representing each of their values. However, whenever one of the 3 bytes is a zero (0) I receive the "net::ERR_EMPTY_RESPONSE" error. The server does not receive the post request when the error occurs and the development tools show a 5s stall.

I've Tried I have tested server side and it is not the issue. I have tested with ajax a bit and anytime I put a zero in the Uint8Array no matter if H is 0 or S, or V. Sending a zero byte causes the error each time. It is not an issue with CORS because the server is handling the CORS preflight properly and a non-zero byte payload data request always works.

Some Code The "getHSVArray" returns a Uint8Array with the HSV values in the range 0-255. Chrome and Firefox allow for Typed Arrays (Uint8Array) to be passed into the XMLHTTPRequest data. The request payload is sent as UTF-8 formatted text. So any zeroes are written as the NUL UTF-8 control character which I believe is causing the issue.

$("#sendBtn").click(() => {
     var byteArray = getHSVArray(kendo.parseColor(colorSelected).toHSV());
     // ex. Uint8Array([255, 255, 255]);
     // error ex. Uint8Array([0, 255, 255]); or Uint8Array([255, 0, 255]);
     $.ajax({
        url: "http://192.168.0.25/",
        type: "POST",
        contentType: false,
        data: byteArray,
        processData: false,
        crossDomain: true
     });
});

I Expect It should send all 3 bytes normally as it does when all 3 HSV bytes are non-zero 1-255 numbers.

HTTP protocol only allows for the transmission of strings. When sending a TypedArray in a post request for example it converts the 0-255 decimals in the array to characters. But a byte of 0x000000 results in a nul control character which causes errors with string variables and processors seeing it as an end to the string. To get past this issue I needed to figure out how to transmit byte arrays using a protocol more like TCP which allows for straight byte data to be sent. The solution was WebSockets which allow for the sending of TypedArrays as base 16 hex bytes. They basically take a Uint8Array from the base 10 input you initialized them with, convert it to base 16, and send it to the server hosting the websocket. Then on the server side you can just convert the bytes back to base 10 and have your client's input values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM