Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么改请求里的response headers里的contentType为application/json #14

Open
JUNHAN12138 opened this issue Nov 27, 2023 · 0 comments

Comments

@JUNHAN12138
Copy link

我在umi框架里写的api接口怎么改请求里的response headers里的contentType为application/json

`import type { UmiApiRequest, UmiApiResponse } from "umi";
import fs from "fs";
import path from "path";

export default async function (req: UmiApiRequest, res: UmiApiResponse) {
switch (req.method) {
case "GET":
try {
const { id } = req.body;
const mdxPath = path.join(
process.cwd(),
"src/mdxFile",
${20231127}.mdx
);

    const mdxContent = fs.readFileSync(mdxPath, "utf8");

    res
      .status(200)
      .header("Content-Type", "application/json")
      .json({ content: mdxContent });
  } catch {
    res.status(500).header("Content-Type", "application/json").json({
      result: false,
      message: "报错",
    });
  }
  break;

}
}`

请求的接口response一直是这个
image
导致fetch的res.json()报错,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant