The value of the Ajax commit parameter with HTML tags cannot be submitted successfully how to resolve

Source: Internet
Author: User
Tags 0xc0
This article mainly introduces the value of AJAX submission parameters with HTML tags can not be submitted a successful solution (ASP), very good, with reference value, the need for reference of friends, hope to help everyone.

This feature is similar to publishing information using a rich text editor, but submitting data with Ajax, so the submission parameter values inevitably contain HTML tags.

Running the code locally has been no problem, can always be submitted successfully, but the code is deployed to the line will not be able to successfully submit data, was the pit for a long time, find long time only to find the problem.

The reason for the unsuccessful submission is that my submission data contains an HTML tag, and then I cannot request it directly to my destination address.

Then the workaround is as follows:

1, in the page with JS BASE64 encoding (like encryption) with the HTML tag parameter values.

2, after the target address obtains the data, uses the background Base64 decoding method to decode the obtained data.

Here is my JS Base64 encoding and decoding method code:

Here are 64 basic encodings of var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; var base64decodechars = new Array (-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1, 1,-1,-1,-1,-1,  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1,-62,-1, 1, 1, 63, 52,  55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 1, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1);    Coded method function Base64Encode (str) {var out, I, Len;     var C1, C2, C3;    len = str.length;    i = 0;    out = "";       while (I < len) {c1 = Str.charcodeat (i++) & 0xFF;             if (i = = len) {out + = Base64encodechars.charat (C1 >> 2); Out + = Base64encodechars.charat (C1 & 0x3) << 4);            Out + = "=";          Break       } C2 = Str.charcodeat (i++);            if (i = = len) {out + = Base64encodechars.charat (C1 >> 2); Out + = Base64encodechars.charat (((C1 & 0x3) << 4) |             ((C2 & 0xF0) >> 4));             Out + = Base64encodechars.charat ((C2 & 0xF) << 2);            Out + = "=";          Break       } C3 = Str.charcodeat (i++);      Out + = Base64encodechars.charat (C1 >> 2); Out + = Base64encodechars.charat (((C1 & 0x3) << 4) |       ((C2 & 0xF0) >> 4)); Out + = Base64encodechars.charat (((C2 & 0xF) << 2) |       ((C3 & 0xC0) >>6));       Out + = Base64encodechars.charat (C3 & 0x3F);   } return out;    }//Decoding method function Base64decode (str) {var c1, C2, C3, C4;     var i, Len, out;     len = str.length;  i = 0;     out = "";        while (I < len) {do {c1 = Base64decodechars[str.charcodeat (i++) & 0xFF]; } WHile (i < len && C1 = =-1);      if (C1 = =-1) break;        do {c2 = Base64decodechars[str.charcodeat (i++) & 0xFF];       } while (I < len && C2 = =-1);      if (C2 = =-1) break; Out + = String.fromCharCode ((C1 << 2) |      ((C2 & 0x30) >> 4));          Do {c3 = Str.charcodeat (i++) & 0xFF;          if (C3 = =) return out;        C3 = Base64decodechars[c3];       } while (I < Len && C3 = =-1);       if (C3 = =-1) break; Out + = String.fromCharCode (((C2 & 0XF) << 4) |     ((C3 & 0x3C) >> 2));           do {C4 = str.charcodeat (i++) & 0xFF;          if (C4 = =) return out;         C4 = base64decodechars[c4];      } while (I < len && C4 = =-1);       if (C4 = =-1) break;      Out + = String.fromCharCode (((C3 & 0x03) << 6) | c4);   } return out;     } function Utf16to8 (str) {var out, I, Len, C; Out= "";    len = str.length;      for (i = 0; i < len; i++) {c = str.charcodeat (i);        if ((c >= 0x0001) && (c <= 0x007F)) {out + = Str.charat (i); } else if (C > 0x07ff) {out + = String.fromCharCode (0xE0 |            ((c >> b) & 0x0F)); Out + = String.fromCharCode (0x80 |           ((c >> 6) & 0x3F)); Out + = String.fromCharCode (0x80 |         ((c >> 0) & 0x3F)); } else {out + = String.fromCharCode (0xC0 |             ((c >> 6) & 0x1F)); Out + = String.fromCharCode (0x80 |          ((c >> 0) & 0x3F));   }} return out;    } function utf8to16 (str) {var out, I, Len, C;    var char2, Char3;    out = "";   len = str.length;    i = 0;    while (I < len) {c = str.charcodeat (i++);          Switch (c >> 4) {case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7://0xxxxxxx          Out + = Str.charat (i-1);         Break Case 12:case 13://110x xxxx 10xx xxxx char2 = str.charcodeat (i++); Out + = String.fromCharCode (((C & 0x1F) << 6) |         (Char2 & 0x3F));        Break          Case://1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i++);           CHAR3 = Str.charcodeat (i++);                  Out + = String.fromCharCode (((C & 0x0F) << 12) |                  ((Char2 & 0x3F) << 6) |          ((Char3 & 0x3F) << 0));        Break  }} return out; }

The page calls the JS method to encode the BASE64 code as follows:

var articlecontent = Editor.getcontent ();   Articlecontent = Base64Encode (Utf16to8 (articlecontent));

But, and encountered a new problem, with JS to the data Base64 encoding, JS incredibly put the plus (+) replaced with a space, resulting in my background method decoded data is incorrect.

The solution is as follows:

I first use the JS method replace () method for replacement, but there are errors, JS just put my first space replaced by a plus (+), the Final solution is as follows.

In the background (under the target accept address) to the data obtained, replace the space with Chengga (+). The code is as follows:

String content = request["Content"]. ToString ();      if (content. Contains (""))        {          content=content. Replace ("", "+");        }      byte[] sa = convert.frombase64string (content);      Encoding Ansi = encoding.getencoding ("GB2312");      Content = ansi.getstring (sa);      Content = base64decrypt (content);//base64 decoding

If the BASE64 encoding and decoding methods in the background are not, see below:

<summary>//BASE64 encryption///</summary>/<param name= "Input" > the string to be encrypted </param>//  <returns></returns> public static string Base64encrypt (string input) {return Base64encrypt (input,    New UTF8Encoding ());    }///<summary>//BASE64 encryption///</summary>/<param name= "Input" > the string to be encrypted </param> <param name= "encode" > Character encoding </param>//<returns></returns> public static string base64e Ncrypt (string input, Encoding encode) {return convert.tobase64string (encode.    GetBytes (input));    }///<summary>//Base64 Decrypt//</summary>/<param name= "Input" > the string to be decrypted </param> <returns></returns> public static string Base64decrypt (string input) {return Base64decrypt (i    Nput, New UTF8Encoding ()); }///<summary>//BASE64 decryption///</summary>/<param name= "Input" > the string to be decrypted </param>//<param name= "encode" > Character encoding </param>//<returns></returns> public Stati C string Base64decrypt (string input, Encoding encode) {return encode.    GetString (convert.frombase64string (input)); }

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.