准备部署一个本地系统,然后请求后端是使用https,结果发现登录的接口没问题,提交数据的接口就出问题了,
问题:
Failed to load resource: the server responded with a status of 500 ()
input.html:1 Failed to load https://127.0.0.1/acquisition...: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access. The response had HTTP status code 500.
使用了jquery 提交;
后台 nginx.conf 部分 配置:
# Begin HTTPS Server
server {
add_header Access-Control-Allow-Origin "http://127.0.0.1";
#add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers "x-requested-with,Authorization";
add_header Access-Control-Allow-Methods *;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log project_error.log;
access_log project_access.log;
} # End HTTPS Server
}
jquery已经在请求头加上 withCredential, crossDomain等字段的;在未部署成localhost本机访问之前都是可用的
请求方法:
$.ajax({
type:"post",
url: "https://127.0.0.1/post",
data: data,
success: function(){},
error: function(){}
})
浏览器的信息显示如下:
你这是跨域问题。请搜索CORS。
需要服务器端逻辑中添加特殊的响应头,允许从其它网站(即跨域)往本服务器发送Ajax请求。
客户端(调用端)是无法解决的。