Would you like to repack the results of the Fetch API library request?

Source: Internet
Author: User
Tags sorts yii
Hello everyone, I use this library https://github.com/dvajs/dva/... When requesting Yii2 Rest API, how can the server return the header x-pagination-page-count, x-pagination-total-countAnd so the information is packaged in return results?

The header of the current API return information already contains the following information

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

I want the returned data to be repackaged so that the data object contains the list (the original return result), the x-pagination-total-count,x-pagination-page-count of the server header information, and so on.

But now I can only get to that the sheer rest of the returned result is a set of arrays. I tried to reseal the data in the request class, and all sorts of attempts failed. A variety of search may be the keyword is wrong and there is no way to find a solution.

May I ask you the great God, how can I pack the objects I need? Thank you!!!

The request code is as follows:

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

The Async method is as follows:

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

The request class is as follows:

  import fetch from ' dva/fetch '; function checkstatus (response) {if (     Response.Status >= && Response.Status < 300) {//This information can be read here. But I won't repack it. Console.log (Response.headers.get (' X-pagination-page-count ')); 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 of we want to request * @param {object} [options] The options we want to pass to "FET Ch "* @return {Object} An object containing either" data "or" ERR "*/export default function request (URL, option s) {return fetch (URL, options). Then (CheckStatus) and then ((data) = ({data})). catch ((err) = > ({err});}  

The rest controller for YII2 is as follows:


  [' class ' = + \yii\filters\cors::classname (), ' Cors ' = [//Restric                    T access to ' Origin ' = [' * '], ' access-control-request-method ' = [' * '], ' Access-control-request-headers ' = [' * '], ' access-control-allow-credentials ' + TR                    UE,//Allow OPTIONS caching ' access-control-max-age ' = 3600,                    The x-pagination-current-page header to is exposed to the browser.    ' Access-control-expose-headers ' = [' x-pagination-current-page '],],], etc.);        Public function Actions () {$actions = Parent::actions ();        Disable the "delete" and "create" operations unset ($actions [' delete '], $actions [' create ']);    return $actions; }}

Reply content:

Hello everyone, I use this library https://github.com/dvajs/dva/... When requesting Yii2 's Rest API, how can I wrap information such as headers returned by the server x-pagination-page-count, x-pagination-total-count into the returned results?

The header of the current API return information already contains the following information

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

I want the returned data to be repackaged so that the data object contains the list (the original return result), the x-pagination-total-count,x-pagination-page-count of the server header information, and so on.

But now I can only get to that the sheer rest of the returned result is a set of arrays. I tried to reseal the data in the request class, and all sorts of attempts failed. A variety of search may be the keyword is wrong and there is no way to find a solution.

May I ask you the great God, how can I pack the objects I need? Thank you!!!

The request code is as follows:

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

The Async method is as follows:

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

The request class is as follows:

  import fetch from ' dva/fetch '; function checkstatus (response) {if (     Response.Status >= && Response.Status < 300) {//This information can be read here. But I won't repack it. Console.log (Response.headers.get (' X-pagination-page-count ')); 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 of we want to request * @param {object} [options] The options we want to pass to "FET Ch "* @return {Object} An object containing either" data "or" ERR "*/export default function request (URL, option s) {return fetch (URL, options). Then (CheckStatus) and then ((data) = ({data})). catch ((err) = > ({err});}  

The rest controller for YII2 is as follows:


  [' class ' = + \yii\filters\cors::classname (), ' Cors ' = [//Restric                    T access to ' Origin ' = [' * '], ' access-control-request-method ' = [' * '], ' Access-control-request-headers ' = [' * '], ' access-control-allow-credentials ' + TR                    UE,//Allow OPTIONS caching ' access-control-max-age ' = 3600,                    The x-pagination-current-page header to is exposed to the browser.    ' Access-control-expose-headers ' = [' x-pagination-current-page '],],], etc.);        Public function Actions () {$actions = Parent::actions ();        Disable the "delete" and "create" operations unset ($actions [' delete '], $actions [' create ']);    return $actions; }}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.