下面是html请求web开发公司服务器接口的示例,是post发送json方式请求。
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <script src="~/lib/jquery-3.3.1/jquery-3.1.1.js"></script> <script src="~/lib/jquery-3.3.1/jquery-3.1.1.min.js"></script></head><body> <fieldset> <legend>身份验证</legend> <form id="login_form"> <label for="UserName">用户名:</label><input type="text" name="userName" id="userName" value="admin" /> <br /> <br /> <label for="Password">密码:</label><input type="password" name="password" id="password" value="123" /> <br /> <br /> </form> <button id="login">登录</button> </fieldset> <script> $(function () { $("#login").on("click", function () { var fields = $('#login_form').serializeArray(); var obj = {}; $.each(fields, function (index, field) { obj[field.name] = field.value; }) $.ajax({ url: "http://localhost:8089/api/Home", data: JSON.stringify(obj), method: "post", dataType: "json", contentType: 'application/json', success: function (data) { console.log(data) if (data.success) { window.token = data.token; alert("登录成功"); } else { alert("登录失败:" + data.message); } } }); }); }); </script></body></html>
- 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