我正在构建一个EC2实例上的web应用程序,使用的是棱角和水瓶。我使用来提供静态HTML文件。Tomcat正在监听端口8080。当第一次加载login.html页面时,将调用Flask上的createState端点,该函数应该在浏览器上设置cookie。我可以看到这个请求是由Flask应用程序接收到的,但是cookie并没有在浏览器上设置。但是,当使用postman测试器调用createState时,我可以看到cookies。下面是相关的代码片段。
createState端点:
@app.route("/createState", methods=["GET", "OPTIONS"])
# Allow cross domain requests from all domains
@cors.crossdomain(origin='*')
def createState():
state = hashlib.sha256(os.urandom(1024)).hexdigest()
session["state"] = state
print("created state token")
return jsonify(1)应用程序运行部分:
if __name__ == "__main__":
app.config['SESSION_COOKIE_NAME'] = 'weatherSess'
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SERVER_NAME'] = "publicDNSofEC2instance:5000"
app.config['PERMANENT_SESSION_LIFETIME'] = 1800
# ------------- Any code other than run goes above this line ---------------#
app.run(host="publicDNSofEC2instance", port=5000, debug=True)
# host is the same as SERVER_NAME(excluding the port number)AngularJS调用createState:
var login = angular .module("authModule",[])
.controller("loginController", function($scope, $http, $window, $sce){
$scope.user = {};
$scope.loginMessage = "";
$http({
method : "GET",
url : instanceURL + "createState"
})
.then(function(response){
// This is a success callback and will be called for status 200-299
console.log(response.data);
if (response.data) {
if(response.data!==1){
$scope.loginMessage = "Sorry. Internal server error."
}
} else {
$scope.loginMessage = "Sorry. Internal server error.";
$scope.user = {};
}
},
function(response){
// This is a failure callback
$scope.loginMessage = "Oops something went wrong. Please try after sometime.";
});我试过了chrome浏览器、firefox浏览器和边缘浏览器。不走运。有人能帮我吗。哦是的..。我甚至尝试使用set_cookie方法。也不起作用。任何帮助都是非常感谢的。
编辑:,所以我刚刚意识到,在AngularJS中处理http请求的方式可能存在一些问题。正如我所说,即使在调用create状态之后,我也看不到在浏览器上设置cookie。但是,当我在浏览器的地址栏中直接访问endpoint(publicDNSofEC2instance:5000/createState),然后重新加载login.html页面时,我可以在浏览器中看到cookies。
这样做的想法是,只要加载页面并设置cookie,就应该立即进行http调用。(我已经尝试删除这个http调用,并根据按一下角键来设置cookie,但这也不起作用。)知道吗,我做错了什么..?
发布于 2016-10-21 22:18:07
MyModule.config(function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
});上面的台词帮我解决了这个问题。因此,一直以来,问题是$http模块默认情况下不会将cookie存储在响应中,也不会将cookie发送到服务器。若要更改此行为,需要设置此标志。
但是,您可能还需要禁用
温多尼
在cookie上标记并使用有效的域名作为标头。
访问-控制-允许-来源
而不是一张“*”牌。
https://stackoverflow.com/questions/40168556
复制相似问题