简体   繁体   English

Vue JS axios向Golang服务器发布请求,预检错误

[英]Vue js axios post request to golang server, Preflight error

I try to send a post api request from a front end website (localhost:8888) to a golang backend (localhost:8000). 我尝试将前端网站(localhost:8888)的api请求发送到golang后端(localhost:8000)。 I get the errors listed below. 我收到下面列出的错误。 I looked on stackoverflow and the problems seems to be cross origin request and preflight request handling. 我查看了stackoverflow,问题似乎是跨源请求和预检请求处理。 I added the headers as shown below but the problem still occurs. 我添加了标题,如下所示,但问题仍然出现。 I hope you guys can help me :) 我希望你们能帮助我:)

Axios error: Axios错误:

OPTIONS http://localhost:8000/api/heimdall/signup 404 (Not Found)

Failed to load http://localhost:8000/api/heimdall/signup: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had HTTP status code 404.

Mux CORS handler Mux CORS处理程序

methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
originsOk := handlers.AllowedOrigins([]string{"*"})
headersOk := handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"})

log.Fatal(http.ListenAndServe(":8000", handlers.CORS(methodsOk, originsOk, headersOk)(r)))

Axois Code: Axois代码:

'use strict';

 var axios = require('axios');

function Signup(email, password) {
var apiURL = 'http://localhost:8000/api/heimdall/signup';

return new Promise((resolve, reject) => {
    axios.post(apiURL, {
        email: email,
        password: password
    })
    .then(respone => {
        console.log('Promise Signup response:', respone);
        resolve(respone);
    }, error => {
        console.log('Promise Signup error:', error);
        reject(error);
    });
});
}

export {
   Signup
};

Maybe you can try this, i use this on my web api. 也许您可以尝试一下,我在我的Web API上使用了它。

var handler http.Handler
{
    handler = handlers.CORS(
        handlers.AllowedOrigins([]string{"*"}),
        handlers.AllowedMethods([]string{"GET", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"}),
        handlers.AllowedHeaders([]string{"Origin", "Authorization", "Content-Type"}),
        handlers.ExposedHeaders([]string{""}),
        handlers.MaxAge(10),
        handlers.AllowCredentials(),
    )(r)
    handler = handlers.RecoveryHandler(handlers.PrintRecoveryStack(true))(handler)
}
http.Handle("/", handler)
http.ListenAndServe(":8080", nil)

I have solved this type of problem in laravel 5.5 (PHP) 我已经在laravel 5.5(PHP)中解决了此类问题

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");

you can try this type headers on golang 你可以在golang上尝试这种类型的标题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM