fetch 簡單使用

 在fetch中第一個爲請求地址,第二個可以設置請求類型POST,GET,DELETE,UPDATE,PATCH和PUT,隨後可以使用then來接收參數,因爲異步操作第一個then標明請求類型,第二個then中可以拿到正確的返回值,catch顯示返回錯誤信息。

fetch('https://ard.hawkwisdom.cn:9001/token/get', {
        method: 'POST',
        body: JSON.stringify({
            username: 'admin',
            password: '*****'
        }),
        headers: { 'Content-Type': 'application/json;charset=utf-8' }
    }).then(response => response.json()).then(data => console.log(data)).catch(err => console.log(err));





fetch("https://api.github.com/users")
            .then((res) => { return res.json(); console.log(res) })
            .then(data => {
                console.log(data);

            })
            .catch(err => console.log(err));

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章