1. getJSON
优点: 简单高效, 直接返回处理好的 JSON 数据
缺点: 只能使用 get 请求和使用 JSON 数据
- <script src ='jquery.min.js'></script>
- <script>
- $(()=>{
- const url='http://xxxxxxxxxx'
- $.getJSON(url,(JSON)=>{
- console.log(JSON)
- })
- })
- </script>
2.Ajax 基本形式
- <script>
- $(() =>{
- const url='http://xxxxxxxx'
- $.Ajax({
- 'url':url,
- // 支持多种请求方式 默认 get, 可以省略
- // 'tupe':'get',
- // 'date':date,
- // 支持多种数据类型 默认 JSON, 可以省略
- // 'dateType':'json',
- 'success':(JSON)=>{
- console.log(JSON)
- }
- // 错误处理 可以省略
- // 'error':(error)=>{}
- })
- })
- </script>
3.Ajax 请求头修改 (带身份令牌)
- <script src="js/jquery.min.js"></script>
- <script>
- $(()=>{
- $.Ajax({
- url:'https://xxxxxxxxxxx',
- // 请求头添加身份令牌
- headers:{"token":"35ad60445cea11eXXXXXXXXXXXXXXXX"},
- success:(JSON)=>{
- console.log(JSON)
- }
- })
- })
- </script>
4. 错误
ERR_CERT_COMMON_NAME_INVALID
可能是浏览器阻止了访问请求
在浏览器的地址栏输入请求的 URL
在阻止页面选择高级 -> 继续访问就 OK
来源: http://www.bubuko.com/infodetail-3358312.html