AJAX带token提交WebApi接口
<html> <head> <title>ajaxTest</title> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> </head> <body> <div id='tData'></div> <input type='button' id='btn' value='测试' /> </body> <script> $(function(){ $('#tData').html('456'); $('#btn').click(function(){ $('#tData').html('开始获取中...'); var token = 'aRhqPBUvX6yYKTkWbzbwFg==' var Data = { printStatus: 3 }; $.ajax({ url: 'http://IP:1003/api/Order/GetOrderListByPage', data: JSON.stringify(Data),//防止谷歌中文乱码 type: 'GET', dataType: 'json', headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, beforeSend: function(request) { //Bearer后面一定要加一个空格 request.setRequestHeader("token",token) }, success: function (data) { console.log(data); $('#tData').html(data); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert('获取数据失败!'); $('#tData').html('获取数据失败!'); } }); }); }) </script> </html>
感谢:https://blog.csdn.net/piaochong2006/article/details/108647577