美文网首页react实习
nextjs 客户端解决cors问题 在设置basepath之后

nextjs 客户端解决cors问题 在设置basepath之后

作者: 左洁 | 来源:发表于2024-05-13 15:18 被阅读0次

1:创建工程文件

npx create-next-app@latest

2:安装 axios


npm install axios

3:设置next.config配置文件


/** @type {import('next').NextConfig} */
const nextConfig = {
    output: 'export',
    basePath: '/test',

    async rewrites() {
        return [
          {
            source: '/api/:path*',
            destination: 'https://domain.com/api/:path*', 
            basePath: false,// 这里的 example.com 是 API 的域名
          },
        ];
      },
};

export default nextConfig;

4:编写请求后台接口代码

'use client'
import exp from "constants";
import Image from "next/image";

 

import axios from 'axios';


export default function Home() {

 

  const test = async()=>{
    const {data} = await axios.post('/api/xxx', {
      user:'2323243'
    }, {
      headers: {
        'Content-Type': 'application/json'
      }
    })
    console.log("Testing=========",data);
  }

  function handleClick() {
    console.log("increment like count")
    test();
  }
  return (
    <main className="bg-white-100">
      <div className="bg-red-800">
      <button onClick={handleClick}>Like</button>
      </div>
    </main>
  );
}

参考文档https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites#rewrites-with-basepath-support

github代码地址

相关文章

  • 如何解决跨域问题

    使用 Java 配置的方式 使用注解的方式 使用 CORS(跨资源共享)解决跨域问题 在header里面设置(在 ...

  • 跨域的解决方案

    1通过cors设置cors头解决跨域,也可以针对一个接口使用cors()中间件解决跨域 2通过设置响应头访问允许控...

  • 跨域方案

    解决跨域问题常用的解决方案: CORS:服务器设置HTTP响应头中Access-Control-Allow-Ori...

  • 解决ajax跨域问题

    Jsonp解决ajax跨域问题 CORS解决ajax跨域问题

  • express 解决跨域

    使用cors中间件解决跨域问题 npm install cors 安装中间件 const cors = requ...

  • Nginx跨域

    Nginx解决跨域问题(CORS) CORS(Cross-Origin Resource Sharing) 跨域资...

  • vue的webpack脚手架开发中使用了代理转发proxyTab

    项目中遇到跨域问题,在不设置CORS跨域的情况下,开发使用代理解决跨域问题。流程: 先获取验证码,直接加载url的...

  • egg

    一 、egg.js之解决跨域问题(egg-cors) 下载 egg-cors 包npm i egg-cors --...

  • Python Django 配置允许跨域 CORS

    Django 跨域问题,解决前后端连接 CORS 安装 django-cors-headers 配置 settin...

  • Spring Boot使用CORS解决跨域问题

    一、跨域问题描述 Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等。CORS 与 ...

网友评论

    本文标题:nextjs 客户端解决cors问题 在设置basepath之后

    本文链接:https://www.haomeiwen.com/subject/lrupfjtx.html