• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

请问使用fetch api库请求 的结果重新包装一下?

php 搞代码 4年前 (2022-01-23) 31次浏览 已收录 0个评论
文章目录[隐藏]

大家好, 我使用这个库https://github.com/dvajs/dva/…请求yii2的rest api时, 如何才能将服务器返回的头部x-pagination-page-count, x-pagination-total-count等信息包装进返回结果呢?

当前api 返回信息的头部已包含下列信息

<code>x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411</code>

我希望将返回的的data重新包装下,使data对象包含list(原来的返回结果), 服务器头信息的x-pagination-total-count,x-pagination-page-count.等信息

但是现在 我只能获取到, 纯粹的rest返回结果 是个数组. 我尝试在 request类中重新封装数据, 各种尝试都失败了. 各种搜 可能是关键词不对 也没有找到解决办法.

请问各位大神, 如何 才能包装成我需要的 对象呢? 谢谢!!!

请求代码如下:

<code> 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,          }        });      }</code>

异步方法如下:

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

request类 如下:

<code>import fetch from 'dva/fetch';function checkStatus(response) {  if (response.status >= 200 && response.status ({data}))    .catch((err) => ({ err }));}</code>

yii2 的rest controller 如下:

<code><?phpnamespace api\modules\test\controllers;use yii;use yii\helpers\ArrayHelper;class UsersController extends yii\rest\ActiveController{    public $modelClass = '\common\models\User';    public function behaviors() {        return ArrayHelper::merge(parent::behaviors(), [            'corsFilter' => [                '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;    }}</code>

回复内容:

大家好, 我使用这个库https://github.com/dvajs/dva/…请求yii2的rest api时, 如何才能将服务器返回的头部x-pagination-page-count, x-pagination-total-count等信息包装进返回结果呢?

当前api 返回信息的头部已包含下列信息

<code>x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411</code>

我希望将返回的的data重新包装下,使data对象包含list(原来的返回结果), 服务器头信息的x-pagination-total-count,x-pagination-page-count.等信息

但是现在 我只能获取到, 纯粹的rest返回结果 是个数组. 我尝试在 request类中重新封装数据, 各种尝试都失败了. 各种搜 可能是关键词不对 也没有找到解决办法.

请问各位大神, 如何 才能包装成我需要的 对象呢? 谢谢!!!

请求代码如下:

<code> 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,          }        });      }</code>

异步方法如下:

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

request类 如下:

<code>import fetch from 'dva/fetch';function checkStatus(response) {  if (response.status >= 200 && response.status ({data}))    .catch((err) => ({ err }));}</code>

yii2 的rest controller 如下:

<code><?phpnamespace api\modules\test\controllers;use yii;use yii\helpers\ArrayHelper;class UsersController extends yii\rest\ActiveController{    public $modelClass = '\common\models\User';    public function behaviors() {        return ArrayHelper::merge(parent::behaviors(), [            'corsFilter' => [                'class' => \yii\filters\Cors::className(),                'cors' => [                    // restrict access to                    'Origin' => ['*'],                    'Access-Control-Request-Method' => ['*'],    <em>/本2文来源[email protected]搞@^&代*@码2网</em><strong>搞gaodaima代码</strong>                '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;    }}</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:请问使用fetch api库请求 的结果重新包装一下?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址