1 //引入jquery
2 <script src="http://localhost:52849/lib/jquery/dist/jquery.min.js"></script>
3 <script>
4 //Promise处理ajax
5 function ajaxAsync(param) {
6 return new Promise((resovle, reject) => {
7 $.ajax({
8 "type": param.type || "get",
9 "url": param.url,
10 "data": param.data || "",
11 "success": res => {
12 resovle(res);
13 },
14 "error": err => {
15 reject(err);
16 }
17 })
18 })
19 }
20 $(async function () {
21 //第一种请求方式
22 try {
23 //await 同步请求
24 var res = await ajaxAsync({
25 url: "http://localhost:52849/MyHome/GetString"
26 })
27 //成功返回值
28 console.log(res);
29 } catch (e) {
30 //失败返回值
31 console.log(e);
32 }
33
34 //第二种请求方式
35 await ajaxAsync({
36 url: "http://localhost:52849/MyHome/GetString"
37 }).then(res => {
38 //成功返回值
39 console.log(res);
40 }).catch(err => {
41 //失败返回值
42 console.log(e);
43 })
44 })
45 </script>