3
3
< head >
4
4
< meta charset ="UTF-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
- < title > JSONP Example</ title >
6
+ < title > JSONP 响应解密示例</ title >
7
+ <!-- 引入 CryptoJS 库 -->
7
8
< script src ="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js "> </ script >
9
+ < style >
10
+ /* 全局样式 */
11
+ body {
12
+ margin : 0 ;
13
+ height : 100vh ;
14
+ display : flex;
15
+ font-family : 'Segoe UI' , Tahoma, Geneva, Verdana, sans-serif;
16
+ color : # 333 ; /* 深色文字 */
17
+ background : linear-gradient (135deg , # ff9a9e, # fad0c4 ); /* 亮色渐变背景 */
18
+ overflow : hidden;
19
+ }
20
+
21
+ /* 分屏布局 */
22
+ .split-container {
23
+ display : flex;
24
+ width : 100% ;
25
+ height : 100% ;
26
+ }
27
+
28
+ /* 左侧操作区 */
29
+ .left-pane {
30
+ flex : 1 ;
31
+ background : rgba (255 , 255 , 255 , 0.3 ); /* 半透明白色背景 */
32
+ backdrop-filter : blur (10px ); /* 毛玻璃效果 */
33
+ padding : 2rem ;
34
+ display : flex;
35
+ flex-direction : column;
36
+ justify-content : center;
37
+ align-items : center;
38
+ border-right : 1px solid rgba (255 , 255 , 255 , 0.5 ); /* 浅色边框 */
39
+ }
40
+
41
+ /* 右侧结果区 */
42
+ .right-pane {
43
+ flex : 1 ;
44
+ background : rgba (255 , 255 , 255 , 0.3 ); /* 半透明白色背景 */
45
+ backdrop-filter : blur (10px ); /* 毛玻璃效果 */
46
+ padding : 2rem ;
47
+ display : flex;
48
+ flex-direction : column;
49
+ justify-content : center;
50
+ align-items : center;
51
+ }
52
+
53
+ /* 标题 */
54
+ h1 {
55
+ margin : 0 0 1.5rem ;
56
+ font-size : 1.8rem ;
57
+ font-weight : 600 ;
58
+ color : # ff6f61 ; /* 亮色标题 */
59
+ }
60
+
61
+ /* 输入框 */
62
+ input {
63
+ width : 100% ;
64
+ max-width : 300px ;
65
+ padding : 0.75rem ;
66
+ font-size : 1rem ;
67
+ border : none;
68
+ border-radius : 8px ;
69
+ background : rgba (255 , 255 , 255 , 0.8 ); /* 半透明白色背景 */
70
+ color : # 333 ; /* 深色文字 */
71
+ outline : none;
72
+ margin-bottom : 1rem ;
73
+ }
74
+
75
+ input ::placeholder {
76
+ color : rgba (51 , 51 , 51 , 0.7 ); /* 深色占位符 */
77
+ }
78
+
79
+ /* 按钮 */
80
+ button {
81
+ width : 100% ;
82
+ max-width : 300px ;
83
+ padding : 0.75rem ;
84
+ font-size : 1rem ;
85
+ border : none;
86
+ border-radius : 8px ;
87
+ background : # ff6f61 ; /* 亮色按钮 */
88
+ color : # fff ; /* 白色文字 */
89
+ cursor : pointer;
90
+ transition : background 0.3s ease;
91
+ }
92
+
93
+ button : hover {
94
+ background : # ff4a3d ; /* 悬停时稍暗的亮色 */
95
+ }
96
+
97
+ /* 结果展示 */
98
+ .result {
99
+ width : 100% ;
100
+ max-width : 500px ;
101
+ height : 300px ;
102
+ padding : 1rem ;
103
+ background : rgba (255 , 255 , 255 , 0.8 ); /* 半透明白色背景 */
104
+ border-radius : 8px ;
105
+ font-size : 0.9rem ;
106
+ color : # 333 ; /* 深色文字 */
107
+ word-wrap : break-word;
108
+ overflow-y : auto;
109
+ border : 1px solid rgba (255 , 255 , 255 , 0.5 ); /* 浅色边框 */
110
+ }
111
+ </ style >
8
112
</ head >
9
113
< body >
10
- < h1 > JSONP Example</ h1 >
11
- < div id ="result "> </ div >
114
+ < div class ="split-container ">
115
+ <!-- 左侧操作区 -->
116
+ < div class ="left-pane ">
117
+ < h1 > JSONP 响应解密</ h1 >
118
+ < input type ="text " id ="inputData " placeholder ="请输入数据 ">
119
+ < button id ="sendRequest "> 发送请求</ button >
120
+ </ div >
12
121
13
- < script >
14
-
15
- // 加密密钥(与后端一致)
16
- const SECRET_KEY = 'CC11001100' ;
122
+ <!-- 右侧结果区 -->
123
+ < div class ="right-pane ">
124
+ < h1 > 解密结果</ h1 >
125
+ < div class ="result " id ="resultContainer "> 结果将显示在这里...</ div >
126
+ </ div >
127
+ </ div >
17
128
18
- // 加密函数
19
- function encryptData ( data ) {
20
- return CryptoJS . AES . encrypt ( JSON . stringify ( data ) , SECRET_KEY ) . toString ( ) ;
129
+ < script >
130
+ // 加密函数(使用 DES)
131
+ function encryptData ( data , secretKey ) {
132
+ return CryptoJS . DES . encrypt ( data , secretKey ) . toString ( ) ;
21
133
}
22
134
23
- // 解密函数
24
- function decryptData ( encryptedData ) {
25
- const bytes = CryptoJS . AES . decrypt ( encryptedData , SECRET_KEY ) ;
135
+ // 解密函数(使用 DES)
136
+ function decryptData ( encryptedData , secretKey ) {
137
+ const bytes = CryptoJS . DES . decrypt ( encryptedData , secretKey ) ;
26
138
return bytes . toString ( CryptoJS . enc . Utf8 ) ;
27
139
}
28
140
29
- // JSONP 请求
30
- function fetchData ( ) {
31
- const data = { name : 'CC11001100' } ;
32
- const encryptedData = encryptData ( data ) ;
33
-
141
+ // JSONP 请求函数
142
+ function jsonpRequest ( url , callbackName , data ) {
34
143
const script = document . createElement ( 'script' ) ;
35
- script . src = `http://localhost:3000/api/data?encryptedData =${ encodeURIComponent ( encryptedData ) } &callback=handleResponse ` ;
144
+ script . src = `${ url } ?callback =${ callbackName } &data= ${ encodeURIComponent ( data ) } ` ;
36
145
document . body . appendChild ( script ) ;
146
+
147
+ // 请求完成后移除 script 标签
148
+ script . onload = function ( ) {
149
+ document . body . removeChild ( script ) ;
150
+ } ;
37
151
}
38
152
39
- // 处理 JSONP 响应
153
+ // 回调函数,用于处理服务器返回的加密数据
40
154
function handleResponse ( encryptedResponse ) {
41
- const decryptedResponse = decryptData ( encryptedResponse ) ;
42
- const responseData = JSON . parse ( decryptedResponse ) ;
155
+ const resultContainer = document . getElementById ( 'resultContainer' ) ;
156
+ const secretKey = "my-secret-key" ; // 解密密钥(需要与服务器一致)
43
157
44
- document . getElementById ( 'result' ) . innerText = responseData . message ;
158
+ try {
159
+ // 解密数据
160
+ const decryptedData = decryptData ( encryptedResponse , secretKey ) ;
161
+ resultContainer . textContent = JSON . stringify ( JSON . parse ( decryptedData ) , null , 2 ) ;
162
+ } catch ( error ) {
163
+ resultContainer . textContent = "解密失败,请检查密钥或数据格式。" ;
164
+ }
45
165
}
46
166
47
- // 发起请求
48
- fetchData ( ) ;
167
+ // 初始化随机值
168
+ function generateRandomValue ( ) {
169
+ const randomData = {
170
+ name : "User" + Math . floor ( Math . random ( ) * 1000 ) ,
171
+ age : Math . floor ( Math . random ( ) * 100 )
172
+ } ;
173
+ return JSON . stringify ( randomData ) ;
174
+ }
175
+
176
+ // 页面加载时初始化输入框
177
+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
178
+ const inputData = document . getElementById ( 'inputData' ) ;
179
+ inputData . value = generateRandomValue ( ) ;
180
+ } ) ;
181
+
182
+ // 绑定按钮点击事件
183
+ document . getElementById ( 'sendRequest' ) . addEventListener ( 'click' , function ( ) {
184
+ const inputData = document . getElementById ( 'inputData' ) . value ;
185
+
186
+ // 发送 JSONP 请求
187
+ jsonpRequest ( "http://localhost:3000/api" , "handleResponse" , inputData ) ;
188
+ } ) ;
49
189
</ script >
50
190
</ body >
51
191
</ html >
0 commit comments