Could you use the fetchapi library to re-encapsulate the request results?

Source: Internet
Author: User
Hello everyone, I am using this library github. comdvajsdva... how can I package the header x-pagination-page-count, x-pagination-total-count, and other information returned by the server into the returned result when requesting the restapi of yii2? The header of the information returned by the current api is... Hello, I am using this library https://github.com/dvajs/dva/...request for the REST api of yii2, how can I send the header returned by the server? x-pagination-page-count, x-pagination-total-countWait for the information to be packaged into the returned results?

The header of the information returned by the current api already contains the following information:
x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411

I want to repackage the returned data so that the data object contains the list (Original returned results), x-pagination-total-count of the server header information, x-pagination-page-count. and other information

But now I can only get it. the pure rest return result is an array. I tried to re-encapsulate the data in the request class, and all the attempts failed. various searches may be incorrect keywords and no solution is found.

How can we wrap it into what I need? Thank you !!!

The request code is as follows:
Const {data} = yield call (query, parse (payload); if (data) {console. log (data );//!!! Here !!! I want to repackage the data returned here so that the data object list, server header information, x-pagination-total-count, x-pagination-page-count. and other information // but now I can only get it. the pure rest return result is an array. I tried to re-encapsulate the data in the request class, and all the attempts failed. various searches may be incorrect keywords and no solution is found. yield put ({type: 'querysuccess', payload: {list: data. list, total: Number (data. total), current: Number (data. current), pageSize: data. pageSize? Data. pageSize: 12 ,}});}
The asynchronous 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> = 200 & response. status <300) {// The information can be read here. but I won't repackage it. 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} 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 }));}
The rest controller of yii2 is as follows:

  ['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 (); // disable the "delete" and "create" operations unset ($ actions ['delete'], $ actions ['create']); return $ actions ;}}

Reply content:

Hello everyone, how can I retrieve the header returned by the server when I use this library https://github.com/dvajs/dva/...request the REST api of yii2?x-pagination-page-count, x-pagination-total-countWait for the information to be packaged into the returned results?

The header of the information returned by the current api already contains the following information:
x-pagination-current-page:1x-pagination-page-count:35x-pagination-per-page:12x-pagination-total-count:411

I want to repackage the returned data so that the data object contains the list (Original returned results), x-pagination-total-count of the server header information, x-pagination-page-count. and other information

But now I can only get it. the pure rest return result is an array. I tried to re-encapsulate the data in the request class, and all the attempts failed. various searches may be incorrect keywords and no solution is found.

How can we wrap it into what I need? Thank you !!!

The request code is as follows:
Const {data} = yield call (query, parse (payload); if (data) {console. log (data );//!!! Here !!! I want to repackage the data returned here so that the data object list, server header information, x-pagination-total-count, x-pagination-page-count. and other information // but now I can only get it. the pure rest return result is an array. I tried to re-encapsulate the data in the request class, and all the attempts failed. various searches may be incorrect keywords and no solution is found. yield put ({type: 'querysuccess', payload: {list: data. list, total: Number (data. total), current: Number (data. current), pageSize: data. pageSize? Data. pageSize: 12 ,}});}
The asynchronous 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> = 200 & response. status <300) {// The information can be read here. but I won't repackage it. 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} 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 }));}
The rest controller of yii2 is as follows:

  ['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 (); // 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.