react项目中利用‘axios‘ 跨域提交表单

import axios from 'axios'
import qs from 'qs'

//这个qs很重要 是转换formData的工具


   axios.post(
      `${cognostest}/bi/v1/disp`,
       qs.stringify(‘这里放你要转换的表单数据,只有这样才能作为表单提交’),
      { headers:{ 'Content-Type':'application/x-www-form-urlencoded' }},
    )

另外还有react跨域的配置方法

修改.roadhogrc.js  这个文件

//这里是我配置的跨域代理

  "proxy": {
    "/api/v1/weather": {
      "target": "https://xxxx.com/",
      "changeOrigin": true,
      "pathRewrite": { "^/api/v1/weather" : "/v3/weather" }
    },
    "/api/v2": {
      "target": "http://localhost:8083",
      "changeOrigin": true,
      "pathRewrite": { "^/api/v2" : "" }
    },
     "/cognostest": {
      "target": "http://xxxxx:9300",
      "changeOrigin": true,
      "pathRewrite": { "^/cognostest" : "" }
    },
  },

这样就能做到跨域提交表单了

猜你喜欢

转载自blog.csdn.net/wangzhichaogege/article/details/108791433