ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Goframe因为axios的header导致的一个BUG解析

2022-01-11 17:05:41  阅读:163  来源: 互联网

标签:axios js header Goframe path import main com


服务器搭建:

go mod init test

go mod edit -require github.com/gogf/gf@latest

package main

import (
    "fmt"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

func main() {
    s := g.Server()
    s.Group("/api", func(group *ghttp.RouterGroup) {
        group.Middleware(MiddlewareCORS)
        // 后台模块
        group.ALL("/test", List)
    })
    s.Run()
}

// MiddlewareCORS 允许跨域
func MiddlewareCORS(r *ghttp.Request) {
    r.Response.CORSDefault()
    r.Middleware.Next()
}

func List(r *ghttp.Request) {
    fmt.Println(r.Get("name"))
    r.Response.Write("hello world")
}

启动服务

 

 

前端使用anxis

main.js

package.json

webpack.config.js

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

Vue.axios.get("http://localhost/api/test").then((response) => {
  console.log(response.data)
})

axios({
     headers: {
        // application/json : 请求体中的数据会以json字符串的形式发送到后端
        // application/x-www-form-urlencoded:请求体中的数据会以普通表单形式(键值对)发送到后端
        // multipart/form-data: 它会将请求体的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。
        'Content-Type': 'application/x-www-form-urlencoded' //参数为object时候请求体中的数据会以普通表单形式(键值对)发送到后端
    },
  method: 'post',
  url: 'http://localhost/api/test',
  params: {
    name: 'jack'
  }
});
const path = require('path');

module.exports = {
  entry: './main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.bundle.js',
  },
};
{
  "scripts": {
    "build": "webpack"
   },
  "dependencies": {
    "axios": "^0.24.0",
    "vue-axios": "^3.4.0",
    "webpack": "^5.4.0",
     "webpack-cli": "^4.2.0"
  }
}

 

执行 

npm install

npm run build 

生成 main.bundle.js

在浏览器console执行

发送请求;

 

 

 

 

 

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:axios,js,header,Goframe,path,import,main,com
来源: https://www.cnblogs.com/xuweiqiang/p/15789072.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有