ajax中使用jsonp方式实现跨域
headers: {'Cookie' : document.cookie } #携带cookie
xhrFields: {
withCredentials: true
}, # 将请求站的cookie一起发送过去
$.ajax(
{
headers: {'Cookie' : document.cookie },
type: 'get',
url: 'http://b.com:5000/login?domain=' + domain +
'&username=' + username,
dateType: 'jsonp',
jsonp: "callback",
xhrFields: {
withCredentials: true
},
success: function (data) {
window.location.href = "http://" + data.domain
}
}
)
Flask视图的设置
@app.route('/login') def login(): domain = request.args.get("domain") username = request.args.get('username') content = { "domain": domain, "msg": 'success', } resp = make_response(jsonify(content)) # 下面这两句允许跨域 resp.headers["Access-Control-Allow-Origin"] = request.headers.get("Origin") resp.headers["Access-Control-Allow-Credentials"] = 'true' resp.set_cookie("username", username) return resp