Simple debugging method for sending binary post data using XMLHttpRequest (Ajax) in firefox3

Source: Internet
Author: User

It is still common for xhr (XMLHttpRequest) to send post text data. You can directly call "xhr. Send (data.

However, the processing of binary data is a little complicated.

 

However, in firefox3, you can use the following method to debug ajax to send post binary data:

Script code:

<SCRIPT type = "text/JavaScript"> <! -- <Br/> function test () {<br/> var DATA = prompt ('Enter the data (grep HEX ):',''); <br/> DATA = eval ('"' + Data + '"'); // parses binary data into a string. <br/> var xhr = new XMLHttpRequest (); <br/> xhr. onreadystatechange = function () {<br/> If (xhr. readystate = 4 & xhr. status = 200) {<br/> alert (xhr. responsetext); <br/>}< br/>}; <br/> var url = '/some. action '; <br/> xhr. open ('post', URL, true); <br/> // xhr. send (data); // This method may not work due to Encoding Restore to raw data. <Br/> xhr. sendasbinary (data ); <br/>}< br/> // --> </SCRIPT> <br/> <input type = "button" value = "ajax" onclick = "test (); "/> 

Auxiliary tool: winhex 15.6

 

Steps:

1. Use winhex to open the binary data file, or directly edit the binary data in winhex.

2. select Edit> copy> grep hex from the winhex menu to copy the binary data to the clipboard in the format of/xnn.

3. Run the script code above and enter the copied data as prompted.

You can use firebug to debug related request data.

 

Note:

Because binary data is not easy to express, we first use winhex to convert the data to "grep hex" text and input it to the script.

The script parses "grep hex" into a two-byte string saved in Javascript. This string is characterized by a one-to-one correspondence between the low byte of each character and each byte of the binary.

In ff, the parameter "data" of the "xhr. Send (data)" method can be a specific object or a string.

For a string, it is converted to binary according to a certain encoding (which is tested to look like a UTF-8), which cannot be restored to the original binary data.

However, FF also provides an "xhr. sendasbinary (data) "method. This method accepts a string as a parameter. It discards the high byte of each character and only keeps the low byte, so that it can be restored to our original data.

 

The implementation of this "tool" is still simple, but it is a little troublesome to use.

For further exploration, it seems that you can select a file through the file dialog box to directly read and send the content, but the implementation is more troublesome.

 

PS: "xhr. sendasbinary (data)" is not a standard method, so it can only be used in ff3.

Refer:

Https://developer.mozilla.org/en/XMLHttpRequest#sendAsBinary ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.