es6 fetch方法请求接口

fetch(url, {
    method: 'post',
    headers: {
        'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8', //设置表单提交的编码方式,php那边才能用$_POST获取数据
        'X-CSRF-TOKEN': $('meta[name=\'csrf_token\']').attr('content')
    },
    body: 'inajax=1&uniStr=' + ext.info.uniStr,
    credentials: 'include'

}).then(function (res) {

    if (res.status === 200) {
    
        res.text().then(function (responseText) {
        
            var objRet = $.parseJSON(responseText);
            if (objRet.code === '0' && objRet.data) {
                //解析接口返回的数据
            }

        });
        
    } else {
        $('#loading').hide();
        window.loading = 1;
    }

    console.log('nextpage is--' + currentPage);

}, function (error) {
    $('#loading').hide();
    window.loading = 1;
});

 

posted on 2017-10-17 15:23  dongruiha  阅读(712)  评论(0)    收藏  举报

导航