axios不向golang api发送POST
我有一个带有negroni中间件的GolangAPI后端 我已经为negroni实现了CORS处理程序,因此我的api应该允许跨源资源共享axios不向golang api发送POST,go,cors,axios,negroni,Go,Cors,Axios,Negroni,我有一个带有negroni中间件的GolangAPI后端 我已经为negroni实现了CORS处理程序,因此我的api应该允许跨源资源共享 // allow OPTIONS method of CORS requests c := cors.New(cors.Options{ AllowedOrigins: []string{"http://127.0.0.1"}, }) //common.StartUp() - Replaced with init method // Get the
// allow OPTIONS method of CORS requests
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://127.0.0.1"},
})
//common.StartUp() - Replaced with init method
// Get the mux router object
router := routers.InitRoutes()
// Create a negroni instance
n := negroni.Classic()
n.Use(c)
n.UseHandler(router)
server := &http.Server{
Addr: common.AppConfig.Server,
Handler: n,
}
log.Println("Listening...")
server.ListenAndServe()
这来自于使用negroni实现CORS的示例
尽管如此,我的api现在将200状态回复回我的前端,但前端不会向服务器发送POST请求。这是我的axios代码:
import axios from 'axios';
const data = {
email: 'user@mail.com',
password: 'secret',
};
export default {
name: 'Login',
methods: {
login() {
axios.post('https://127.0.0.1:8090/users/login', data);
},
邮递员在发送POST请求时没有任何问题。我做错了什么?好的,我找到了解决问题的方法: 如本文所述,我为cors negroni插件添加了更多选项。我的申请中缺少的一个重要选项是行
AllowedHeaders: []string{"X-Auth-Key", "X-Auth-Secret", "Content-Type"},
因为我的应用程序发送了内容类型标题,而api拒绝了它
我希望这能帮助其他有类似问题的人。typo<代码>允许的来源:[]字符串{”http://172.0.0.1“}可能是127.0.0.1首先,您正在访问127.0.0.1的站点,但您正在将原点设置为172.0.0.1。哦,我的错误。。。我更改了它,但它仍然不工作。您在浏览器的控制台中是否有任何错误消息?此外,您在浏览器中有
http
,在js代码中有go代码,但在js代码中有https
。http://localhost
和http://localhost:8080
是不同的来源。实际上,JS控制台中应该有一条消息。