嗨,我正在实施rest apis,为此我想要允许交叉原始请求.
我目前在做什么:
AWS上的Go-server代码:
func (c *UserController) Login(w http.ResponseWriter, r *http.Request, ctx *rack.Context) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
...
...
c.render.Json(w,rsp, http.StatusOK)
return
}
Run Code Online (Sandbox Code Playgroud)
localhost上的Ajax代码:
<script>
$( document ).ready(function() {
console.log( "ready!" );
$.ajax({
url: 'http://ip:8080/login',
crossDomain: true, //set as a cross domain requests
withCredentials:false,
type: 'post',
success: function (data) {
alert("Data " + data);
},
});
});
Run Code Online (Sandbox Code Playgroud)
我在浏览器控制台上收到以下错误: XMLHttpRequest无法加载http:// ip:8080/login.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:8081 '访问.响应具有HTTP状态代码422.
我尝试添加预检选项:
func corsRoute(app *app.App) {
allowedHeaders := "Accept, Content-Type, Content-Length, …Run Code Online (Sandbox Code Playgroud)