鍍金池/ 問答/Linux  HTML/ fetch請求后的問題

fetch請求后的問題

fetch封裝參考
http://gitbook.cn/books/59259...
引用封裝好的fetch
clipboard.png

在vue中調(diào)用方法
clipboard.png

network中是返回了數(shù)據(jù),但是當(dāng)我用data接收數(shù)據(jù)并打印出來是失敗的

clipboard.png

clipboard.png

是否有大佬知道問題出現(xiàn)在哪里??請教下?。?/p>

回答
編輯回答
尛曖昧

get拿到了只讀數(shù)據(jù)

2018年4月8日 02:56
編輯回答
兔囡囡

你這樣寫,肯定獲取不到數(shù)據(jù)了,得到的是promise對(duì)象,axios是用promise封裝的,要用then才能獲取到數(shù)據(jù),
需要這樣寫
await filter().then((res) => { console.log(res) })

2018年5月20日 12:43
編輯回答
有你在

如果fetch是我以為的那樣的話, 可以改寫這個(gè)代碼:

let data = await filter().then(res => res.json()))
console.log(data)
2018年6月9日 22:42
編輯回答
有點(diǎn)壞

是不是沒有res.json()

2017年9月9日 12:56
編輯回答
下墜

看不出來有什么不對(duì),你確定這個(gè) response 對(duì)象是 data 打印的?

2017年3月15日 05:22
編輯回答
兔囡囡

這是樓主提供的文章里fetch的封裝函數(shù),
這個(gè)是有明顯問題的,當(dāng)系統(tǒng)支持fetch 并使用的時(shí)候返回的值已經(jīng)是結(jié)果了,并不是一個(gè)promise,所以
你的代碼中再使用await 肯定是行不通的,但這個(gè)函數(shù)最大的問題就是,如果當(dāng)檢測到,系統(tǒng)不支持fetch ,改用XMLHttpRequest 它的返回值又是promise,
應(yīng)該統(tǒng)一返回promise,看我在代碼中注釋部分

export default async(type = 'GET', url = '', data = {}, method = 'fetch') => {
    type = type.toUpperCase();
    url = baseUrl + url;
    if (type == 'GET') {
        let dataStr = ''; 
        Object.keys(data).forEach(key => {
            dataStr += key + '=' + data[key] + '&';
        })

        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
            url = url + '?' + dataStr;
        }
    }

    if (window.fetch && method == 'fetch') {
        let requestConfig = {
            credentials: 'include',
            method: type,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            mode: "cors",
            cache: "force-cache"
        }

        if (type == 'POST') {
            Object.defineProperty(requestConfig, 'body', {
                value: JSON.stringify(data)
            })
        }
        // 如果樓主想要在代碼中使用await 那只要在這里返回fetch就可以了,
        //return fetch(url, requestConfig);
        //從下面一行,到else 上面部分的代碼是不需要的
        try {
            var response = await fetch(url, requestConfig);
            var responseJson = await response.json();
        } catch (error) {
            throw new Error(error)
        }
        return responseJson
    } else {
        return new Promise((resolve, reject) => {
            let requestObj;
            if (window.XMLHttpRequest) {
                requestObj = new XMLHttpRequest();
            } else {
                requestObj = new ActiveXObject;
            }

            let sendData = '';
            if (type == 'POST') {
                sendData = JSON.stringify(data);
            }

            requestObj.open(type, url, true);
            requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            requestObj.send(sendData);

            requestObj.onreadystatechange = () => {
                if (requestObj.readyState == 4) {
                    if (requestObj.status == 200) {
                        let obj = requestObj.response
                        if (typeof obj !== 'object') {
                            obj = JSON.parse(obj);
                        }
                        resolve(obj)
                    } else {
                        reject(requestObj)
                    }
                }
            }
        })
    }
}
2017年4月13日 03:11