问题的背景是:在写nextjs时,遇到的请求接口数据报的错
错误的原因是:调用url时没有加jsononly = 1
import { useState } from 'react';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
// ...
export default function FriendCenterNextTest({data}: InferGetServerSidePropsType<typeof getServerSideProps>) {
console.log('-------------fet', data);
// ...
return (
// ...
);
}
export const getServerSideProps: GetServerSideProps = async () => {
// 要注意,这里定义好的httpConf.node_host形式类似于https://xxx
const host = isNotEnv ? httpConf.node_host : httpConf.local_host;
const url = `${host}/xxx?${qs.stringify({
jsononly: 1, // !是这里,需要加jsononly = 1
})};`;
let resData = null;
await fetch(url)
.then((res) => res.json())
.then((body) => {
resData = body;
})
.catch((e) => console.error('-----fetch error', e));
return {
props: {
post: resData,
data: resData?.data,
},
};
}