Przeglądaj źródła

登录通过axios发起请求

locky 1 rok temu
rodzic
commit
7e48fa0a24
2 zmienionych plików z 44 dodań i 52 usunięć
  1. 13 28
      templates/login.html
  2. 31 24
      templates/login_VSees.html

+ 13 - 28
templates/login.html

@@ -324,37 +324,22 @@
             //console.log("user: " + user.value);
             //console.log("pwd: " + pwd.value);
 
-            var params = {
-                user: user.value,
-                pwd: pwd.value,
-                state: {{ state }},
-                client_id: {{ client_id }},
-                response_type: {{ response_type }},
-                scope: {{ scope }},
-                redirect_uri: {{ redirect_uri }},
-                skill_name: {{ skill_name }}
-            };
-            $.ajax({
-                url: "/oa2/login",
-                type: "GET",
-                data: params,
-                dataType: "json", // 假设服务器返回 JSON 格式的数据
-                success: function(data) {
-                    hideLoadingIcon();
-                    if (data.code === 0) {
-                        console.log('request start');
-                        window.location = data.res;
-                    } else {
-                        alert(data.msg);
-                    }
-                },
-                error: function(xhr, textStatus, error) {
-                    hideLoadingIcon();
-                    alert("Error: " + textStatus);
+            var params = ("user=" + user.value + "&pwd=" + pwd.value + "&state={{ state }}" + "&client_id={{ client_id }}"
+                + "&response_type={{ response_type }}" + "&scope={{ scope }}" + "&redirect_uri={{ redirect_uri }}"
+                + "&skill_name={{ skill_name }}");
+            $.get("/oa2/login", params, function (reUrl) {
+                //alert(reUrl);
+                let data = JSON.parse(reUrl)
+                if(data['code']===0){
+                    console.log('request start')
+                    window.location = data['res'];
+                }else{
+                    alert(data['msg'])
                 }
+								hideLoadingIcon();
             });
         } else {
-            alert("Please input user and password");
+            alert("please input user and password");
         }
     }
 </script>

+ 31 - 24
templates/login_VSees.html

@@ -8,6 +8,8 @@
     <title>VSees Login</title>
     <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
 		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-..." crossorigin="anonymous" />
+		<!-- 引入 axios 的 JS 文件 -->
+  	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
 </head>
 <style type="text/css">
 	html,body{
@@ -320,33 +322,38 @@
         var user = document.getElementById("user_text");
         var pwd = document.getElementById("password_text");
         if (user.value.length > 0) {
-						showLoadingIcon();
-            //console.log("user: " + user.value);
-            //console.log("pwd: " + pwd.value);
-
-            var params = ("user=" + user.value + "&pwd=" + pwd.value + "&state={{ state }}" + "&client_id={{ client_id }}"
-                + "&response_type={{ response_type }}" + "&scope={{ scope }}" + "&redirect_uri={{ redirect_uri }}"
-                + "&skill_name={{ skill_name }}");
-						$.get("/oa2/login", params)
-							.done(function (reUrl) {
-								let data = JSON.parse(reUrl);
-								if (data['code'] === 0) {
-									console.log('request start');
-									window.location = data['res'];
-								} else {
-									alert(data['msg']);
-								}
-								hideLoadingIcon();
-							})
-							.fail(function (xhr, textStatus, error) {
-								// 请求失败的处理
-								hideLoadingIcon();
-								alert(textStatus);
-							});
+            const params = {
+                user: user.value,
+                pwd: pwd.value,
+                state: '{{ state }}',
+                client_id: '{{ client_id }}',
+                response_type: '{{ response_type }}',
+                scope: '{{ scope }}',
+                redirect_uri: '{{ redirect_uri }}',
+                skill_name: '{{ skill_name }}'
+            };
+            showLoadingIcon();
+            axios.get('/oa2/login', { params: params })
+                .then(response => {
+                    // 请求成功处理
+                    hideLoadingIcon();
+                    const data = response.data;
+                    if (data['code'] === 0) {
+                        console.log('request start');
+                        window.location = data['res'];
+                    } else {
+                        alert(data['msg']);
+                    }
+                })
+                .catch(error => {
+                    // 请求失败处理
+                    hideLoadingIcon();
+                    alert(error);
+                });
         } else {
             alert("please input user and password");
         }
-    }
+}
 </script>