Home > Web Front-end > JS Tutorial > The solution to the problem that the Ajax submission parameter value contains html tags and cannot be submitted successfully

The solution to the problem that the Ajax submission parameter value contains html tags and cannot be submitted successfully

亚连
Release: 2018-05-23 10:19:23
Original
2316 people have browsed it

This article mainly introduces the solution to the problem that the value of the Ajax submission parameter with the html tag cannot be submitted successfully. It is very good and has reference value. Friends who need it can refer to it

I have been working as a resource in the company recently. And the article upload function encountered a small problem.

This function is similar to using a rich text editor to publish information, but uses Ajax to submit data, so the submitted parameter value inevitably contains html tags.

There has been no problem running the code locally and the submission can always be successful. However, when the code is deployed online, the data cannot be submitted successfully. I was stuck for a long time and it took me a long time to find the problem.

The reason why the submission was unsuccessful is because my submission data contains html tags, and my target address cannot be directly requested.

# Then the solution is as follows:

1. Use JS's base64 coding (similar to encryption) on the page with the parameter value of HTML tags.

2. After obtaining the data at the target address, use the background Base64 decoding method to decode the obtained data.

The following is the Base64 encoding and decoding method code of my JS:

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

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

//下面是64个基本的编码

 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, -1, -1, -1, -1, 62, -1, -1, -1, 63,

   52, 53, 54, 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, 39, 40,

   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

 //编码的方法

 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;

   }

 //解码的方法

 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 == 61)

            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 == 61)

            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 >> 12) & 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 14:

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

  }

Copy after login

The page calls the JS method to perform Base64 encoding code as follows:

1

2

var articleContent = editor.getContent();

  articleContent = base64encode(utf16to8(articleContent));

Copy after login

But, I encountered another one New problem. After using JS to Base64 encode the data, JS actually replaced the plus sign ( ) with a space, causing the data decoded by my background method to be incorrect.

The solution is as follows:

I first used the JS method replace() method to replace, but there was an error. JS just replaced my first A space is replaced with a plus sign ( ). The final solution is as follows.

In the background (under the target receiving address), replace the spaces with plus signs ( ) for the obtained data. The code is as follows:        

1

2

3

4

5

6

7

8

9

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解码

Copy after login

                                                                                                                                out ’ ’ way ’s ’ way out ’ s ’ way out ’’ out ’ out ’ ’ ’ ’ ’ out ’est ’ out out ’ ’ way ’ out way out est est out out out out out out out out favor out deep in mom out out out out out out out out out out Out Out out Off i t t o t- t i t i t e t e i e s t o ld y yep

Related articles:

Ajax post request jump page

Ajax submission form page refreshes quickly solution

Two solutions for Ajax opening a new window and being intercepted by the browser

The above is the detailed content of The solution to the problem that the Ajax submission parameter value contains html tags and cannot be submitted successfully. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template