鍍金池/ 問答/PHP  HTML/ 使用Fetch從服務(wù)器上獲取數(shù)據(jù),有的時候返回是Promise對象,有的時候返回

使用Fetch從服務(wù)器上獲取數(shù)據(jù),有的時候返回是Promise對象,有的時候返回一個json對象?

我使用下面這段代碼從服務(wù)器上獲取數(shù)據(jù),有的時候返回是Promise對象,有的時候返回一個json對象,我怎么寫才能保證一定返回json對象?

postFromServer ( requestParam ) {
        const _self = this;
        let url = requestParam['url'] || '';
        let headers = requestParam['headers'] || '';
        let postBody = requestParam['postBody'] || '';
        let code = requestParam['code'] || 200;
        let consoleMessage = requestParam['consoleMessage'] || false;
        let response = _self.sendToServer( url, 'POST', headers, postBody );

        return response.then(function(value) {
            if ( consoleMessage === true ) {
                console.log('common-helper-es6 postFromServer === ', value)
            }
            return value;
        }, function(error) {
            console.log('postFromServer error === ', error)
        });
    },
回答
編輯回答
陌上花

Promise.then(res=>return res.json()).then(res=>console.log(res));
第一次then的時候轉(zhuǎn)JOSN格式,第二次的then的時候就可以得到這個對象了

2018年3月10日 16:25