GET请求
GET请求:登录、上传、刷新、删除
HTML:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 5 <title>示范</title> 6 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> 7 <script src="./main.js"></script> 8 </head> 9 10 <body> 11 <input id='u' /> 12 <input id='p' /> 13 <button onclick="foo()">login</button> 14 <br /> 15 <input id='t' /> 16 <input id='c' /> 17 <button onclick="upload()">upload</button> 18 <br /> 19 <button onclick="fresh()">fresh</button> 20 <button onclick="clean()">clean</button> 21 <div id='target'></div> 22 </body> 23 </html>
js:
1 const targetURL = 'http://kod.yxy.moyinzi.top/data/Group/public/home/share/18/api' 2 3 function foo() { 4 const u = $('#u').val() 5 const p = $('#p').val() 6 $.ajax({ 7 type: "GET", 8 url: `${targetURL}/login.php?username=${u}&password=${p}`, 9 dataType: "json", 10 success: (res) => { 11 if (res.code === 0) { 12 alert(res.msg) 13 } else { 14 alert(res.msg) 15 } 16 }, 17 }) 18 } 19 20 function fresh() { 21 $.ajax({ 22 type: "GET", 23 url: `${targetURL}/list.php`, 24 dataType: "json", 25 success: (res) => { 26 if (res.code === 0) { 27 let content = "" 28 for (let i in res.data) { 29 const data = res.data[i] 30 content += `<div> 31 <h1>${data.title}</h1> 32 <p>${data.content}</p> 33 </div>` 34 } 35 $("#target").html(`<div>${content}</div>`) 36 } else { 37 alert(res.msg) 38 } 39 }, 40 }) 41 } 42 43 function upload() { 44 const t = $('#t').val() 45 const c = $('#c').val() 46 $.ajax({ 47 type: "GET", 48 url: `${targetURL}/upload.php?title=${t}&content=${c}`, 49 dataType: "json", 50 success: (res) => { 51 if (res.code === 0) { 52 alert(res.msg) 53 } else { 54 alert(res.msg) 55 } 56 }, 57 }) 58 } 59 60 function clean() { 61 $.ajax({ 62 type: "GET", 63 url: `${targetURL}/logout.php`, 64 dataType: "json", 65 success: (res) => { 66 if (res.code === 0) { 67 alert(res.msg) 68 } else { 69 alert(res.msg) 70 } 71 }, 72 }) 73 }
data.json:
1 { 2 "code": 0, 3 "msg": "SUCCESS", 4 "data": [ 5 { 6 "title": "王思源的任务进度", 7 "content": "30%" 8 }, { 9 "title": "周嵩", 10 "content": "60%" 11 }, { 12 "title": "测试数据", 13 "content": "以上数据仅为测试,不要当真" 14 } 15 ] 16 }
data2.json:
1 { 2 "code": -1, 3 "msg": "发生异常", 4 "data": null 5 }
附加API:
list.php
1 <?php session_start(); 2 header('Access-Control-Allow-Origin:*'); 3 class Response { 4 public $code = -1; 5 public $msg = 'FAIL'; 6 public $data; 7 } 8 9 if (!isset($_SESSION['vlogin'])) { 10 $res = new Response(); 11 $res->code = -2; 12 $res->msg = 'LOGOUT'; 13 echo json_encode($res); 14 return; 15 } 16 17 if (isset($_SESSION['vdata'])) { 18 $vdata = $_SESSION['vdata']; 19 } else { 20 $vdata = array(); 21 } 22 23 $res = new Response(); 24 $res->code = 0; 25 $res->msg = 'SUCCESS'; 26 $res->data = $vdata; 27 echo json_encode($res);
login.php
1 <?php session_start(); 2 header('Access-Control-Allow-Origin:*'); 3 class Response { 4 public $code = -1; 5 public $msg = 'FAIL'; 6 public $data; 7 } 8 9 $username = $_GET['username'] ? $_GET['username'] : $_POST['username']; 10 $password = $_GET['password'] ? $_GET['password'] : $_POST['password']; 11 12 if (($username=='stu') && ($password=='123')) { 13 $res = new Response(); 14 $res->code = 0; 15 $res->msg = 'SUCCESS'; 16 $_SESSION['vlogin'] = 1; 17 echo json_encode($res); 18 } else { 19 echo json_encode(new Response()); 20 }
logout.php
1 <?php session_start(); 2 header('Access-Control-Allow-Origin:*'); 3 class Response { 4 public $code = -1; 5 public $msg = 'FAIL'; 6 public $data; 7 } 8 9 unset($_SESSION['vlogin']); 10 unset($_SESSION['vdata']); 11 12 $res = new Response(); 13 $res->code = 0; 14 $res->msg = 'SUCCESS'; 15 $res->data = $vdata; 16 echo json_encode($res);
upload.php
1 <?php session_start(); 2 header('Access-Control-Allow-Origin:*'); 3 class Response { 4 public $code = -1; 5 public $msg = 'FAIL'; 6 public $data; 7 } 8 9 if (!isset($_SESSION['vlogin'])) { 10 $res = new Response(); 11 $res->code = -2; 12 $res->msg = 'LOGOUT'; 13 echo json_encode($res); 14 return; 15 } 16 17 if (isset($_SESSION['vdata'])) { 18 $vdata = $_SESSION['vdata']; 19 } else { 20 $vdata = array(); 21 } 22 array_push($vdata, $_POST ? $_POST : $_GET); 23 $_SESSION['vdata'] = $vdata; 24 25 $res = new Response(); 26 $res->code = 0; 27 $res->msg = 'SUCCESS'; 28 echo json_encode($res);
const targetURL = 'http://kod.yxy.moyinzi.top/data/Group/public/home/share/18/api'
function foo() {
const u = $('#u').val()
const p = $('#p').val()
$.ajax({
type: "GET",
url: `${targetURL}/login.php?username=${u}&password=${p}`,
dataType: "json",
success: (res) => {
if (res.code === 0) {
alert(res.msg)
} else {
alert(res.msg)
}
},
})
}
function fresh() {
$.ajax({
type: "GET",
url: `${targetURL}/list.php`,
dataType: "json",
success: (res) => {
if (res.code === 0) {
let content = ""
for (let i in res.data) {
const data = res.data[i]
content += `<div>
<h1>${data.title}</h1>
<p>${data.content}</p>
</div>`
}
$("#target").html(`<div>${content}</div>`)
} else {
alert(res.msg)
}
},
})
}
function upload() {
const t = $('#t').val()
const c = $('#c').val()
$.ajax({
type: "GET",
url: `${targetURL}/upload.php?title=${t}&content=${c}`,
dataType: "json",
success: (res) => {
if (res.code === 0) {
alert(res.msg)
} else {
alert(res.msg)
}
},
})
}
function clean() {
$.ajax({
type: "GET",
url: `${targetURL}/logout.php`,
dataType: "json",
success: (res) => {
if (res.code === 0) {
alert(res.msg)
} else {
alert(res.msg)
}
},
})
}