时间:2021-07-01 10:21:17 帮助过:39人阅读
x-pagination-page-count, x-pagination-total-count
等信息包装进返回结果呢?
x-pagination-current-page:1
x-pagination-page-count:35
x-pagination-per-page:12
x-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)}`);
}
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 }));
}
[
'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
等信息包装进返回结果呢?
x-pagination-current-page:1
x-pagination-page-count:35
x-pagination-per-page:12
x-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)}`);
}
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 }));
}
[
'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;
}
}