html ajax 异步加载 局部刷新

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
在阻止页面选择高级->继续访问就OKjquery

相关文章
相关标签/搜索