請問使用fetch api程式庫要求 的結果重新封裝一下?

來源:互聯網
上載者:User
大家好, 我使用這個庫https://github.com/dvajs/dva/...請求yii2的rest api時, 如何才能將伺服器返回的頭部 x-pagination-page-count, x-pagination-total-count等資訊封裝進返回結果呢?

當前api 返回資訊的頭部已包含下列資訊

x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411

我希望將返回的的data重新封裝下,使data對象包含list(原來的返回結果), 伺服器頭資訊的x-pagination-total-count,x-pagination-page-count.等資訊

但是現在 我只能擷取到, 純粹的rest返回結果 是個數組. 我嘗試在 request類中重新封裝資料, 各種嘗試都失敗了. 各種搜 可能是關鍵詞不對 也沒有找到解決辦法.

請問各位大神, 如何 才能封裝成我需要的 對象呢? 謝謝!!!

請求代碼如下:

 const {data} = yield call(query, parse(payload));      if (data) {        console.log(data); //!!!這裡!!!  我希望將這裡返回的data 重新封裝下,使data對象list, 伺服器頭資訊,x-pagination-total-count,x-pagination-page-count.等資訊         // 但是現在 我只能擷取到, 純粹的rest返回結果 是個數組. 我嘗試在 request類中重新封裝資料, 各種嘗試都失敗了. 各種搜  可能是關鍵詞不對  也沒有找到解決辦法.         yield put({          type: 'querySuccess',          payload: {            list: data.list,            total: Number(data.total),            current: Number(data.current),            pageSize: data.pageSize ? data.pageSize : 12,          }        });      }

非同步方法呼叫如下:

export async function query(params) {  return request(`/api/users?${qs.stringify(params)}`);}

request類 如下:

import fetch from 'dva/fetch';function checkStatus(response) {  if (response.status >= 200 && response.status < 300) {    //這裡是可以讀取到這些資訊的. 但是我不會重新封裝.     console.log(response.headers.get('x-pagination-page-count')); //35    console.log(response.headers.get('x-pagination-total-count')); //411    return response;  }  const error = new Error(response.statusText);  error.response = response;  throw error;}function parseJSON(response) {  return response.json();}/** * Requests a URL, returning a promise. * * @param  {string} url       The URL we want to request * @param  {object} [options] The options we want to pass to "fetch" * @return {object}           An object containing either "data" or "err" */export default function request(url, options) {  return fetch(url, options)    .then(checkStatus)    .then(parseJSON)    .then((data)=>({data}))    .catch((err) => ({ err }));}

yii2 的rest controller 如下:

 [                'class' => \yii\filters\Cors::className(),                'cors' => [                    // restrict access to                    'Origin' => ['*'],                    'Access-Control-Request-Method' => ['*'],                    'Access-Control-Request-Headers' => ['*'],                    'Access-Control-Allow-Credentials' => true,                    // Allow OPTIONS caching                    'Access-Control-Max-Age' => 3600,                    // Allow the X-Pagination-Current-Page header to be exposed to the browser.                    'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],                ],            ],        ]);    }    public function actions()    {        $actions = parent::actions();        // 禁用"delete" 和 "create" 操作        unset($actions['delete'], $actions['create']);        return $actions;    }}

回複內容:

大家好, 我使用這個庫https://github.com/dvajs/dva/...請求yii2的rest api時, 如何才能將伺服器返回的頭部x-pagination-page-count, x-pagination-total-count等資訊封裝進返回結果呢?

當前api 返回資訊的頭部已包含下列資訊

x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411

我希望將返回的的data重新封裝下,使data對象包含list(原來的返回結果), 伺服器頭資訊的x-pagination-total-count,x-pagination-page-count.等資訊

但是現在 我只能擷取到, 純粹的rest返回結果 是個數組. 我嘗試在 request類中重新封裝資料, 各種嘗試都失敗了. 各種搜 可能是關鍵詞不對 也沒有找到解決辦法.

請問各位大神, 如何 才能封裝成我需要的 對象呢? 謝謝!!!

請求代碼如下:

 const {data} = yield call(query, parse(payload));      if (data) {        console.log(data); //!!!這裡!!!  我希望將這裡返回的data 重新封裝下,使data對象list, 伺服器頭資訊,x-pagination-total-count,x-pagination-page-count.等資訊         // 但是現在 我只能擷取到, 純粹的rest返回結果 是個數組. 我嘗試在 request類中重新封裝資料, 各種嘗試都失敗了. 各種搜  可能是關鍵詞不對  也沒有找到解決辦法.         yield put({          type: 'querySuccess',          payload: {            list: data.list,            total: Number(data.total),            current: Number(data.current),            pageSize: data.pageSize ? data.pageSize : 12,          }        });      }

非同步方法呼叫如下:

export async function query(params) {  return request(`/api/users?${qs.stringify(params)}`);}

request類 如下:

import fetch from 'dva/fetch';function checkStatus(response) {  if (response.status >= 200 && response.status < 300) {    //這裡是可以讀取到這些資訊的. 但是我不會重新封裝.     console.log(response.headers.get('x-pagination-page-count')); //35    console.log(response.headers.get('x-pagination-total-count')); //411    return response;  }  const error = new Error(response.statusText);  error.response = response;  throw error;}function parseJSON(response) {  return response.json();}/** * Requests a URL, returning a promise. * * @param  {string} url       The URL we want to request * @param  {object} [options] The options we want to pass to "fetch" * @return {object}           An object containing either "data" or "err" */export default function request(url, options) {  return fetch(url, options)    .then(checkStatus)    .then(parseJSON)    .then((data)=>({data}))    .catch((err) => ({ err }));}

yii2 的rest controller 如下:

 [                'class' => \yii\filters\Cors::className(),                'cors' => [                    // restrict access to                    'Origin' => ['*'],                    'Access-Control-Request-Method' => ['*'],                    'Access-Control-Request-Headers' => ['*'],                    'Access-Control-Allow-Credentials' => true,                    // Allow OPTIONS caching                    'Access-Control-Max-Age' => 3600,                    // Allow the X-Pagination-Current-Page header to be exposed to the browser.                    'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],                ],            ],        ]);    }    public function actions()    {        $actions = parent::actions();        // 禁用"delete" 和 "create" 操作        unset($actions['delete'], $actions['create']);        return $actions;    }}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.