如何从 Next.js 中的 WP REST API 获得响应 headers?
How to get response headers from WP REST API in Next.js?
我正在使用 Isomorphic unfetch 从 Next.js 中的 WP REST API 获取一些数据,我怎样才能得到响应 headers?
import fetch from 'isomorphic-unfetch';
class Blog extends React.Component {
static async getInitialProps() {
const res = await fetch('https://wordpress.org/news/wp-json/wp/v2/posts');
const data = await res.json();
return {
res: res,
data: data
};
}
render() {
console.log(this.props.res)
console.log(this.props.data)
return (
<h1>Blog</h1>
)
}
}
我在控制台中看不到任何可用的内容
但是 headers 在我刚打开时就在那里 url in browser
res.headers 将 return 全部 headers 根据 this。您可以使用 res.headers.get('x-wp-total') 获取值,然后在 getInitialProps 函数中 return 它。
import fetch from 'isomorphic-unfetch';
class Blog extends React.Component {
static async getInitialProps() {
const res = await fetch('https://wordpress.org/news/wp-json/wp/v2/posts');
const data = await res.json();
return {
res: res,
data: data,
wpTotal: res.headers.get('x-wp-total')
};
}
render() {
console.log(this.props.wpTotal)
console.log(this.props.data)
return (
<h1>Blog</h1>
)
}
}
我正在使用 Isomorphic unfetch 从 Next.js 中的 WP REST API 获取一些数据,我怎样才能得到响应 headers?
import fetch from 'isomorphic-unfetch';
class Blog extends React.Component {
static async getInitialProps() {
const res = await fetch('https://wordpress.org/news/wp-json/wp/v2/posts');
const data = await res.json();
return {
res: res,
data: data
};
}
render() {
console.log(this.props.res)
console.log(this.props.data)
return (
<h1>Blog</h1>
)
}
}
我在控制台中看不到任何可用的内容
但是 headers 在我刚打开时就在那里 url in browser
res.headers 将 return 全部 headers 根据 this。您可以使用 res.headers.get('x-wp-total') 获取值,然后在 getInitialProps 函数中 return 它。
import fetch from 'isomorphic-unfetch';
class Blog extends React.Component {
static async getInitialProps() {
const res = await fetch('https://wordpress.org/news/wp-json/wp/v2/posts');
const data = await res.json();
return {
res: res,
data: data,
wpTotal: res.headers.get('x-wp-total')
};
}
render() {
console.log(this.props.wpTotal)
console.log(this.props.data)
return (
<h1>Blog</h1>
)
}
}