大家好, 我使用這個庫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; }}