只能访问到登录界面,跳转不成功的问题

这个问题困扰了我一个多星期,始终无法解决,确定后端代码已经完全正确的情况下,检查了前端代码,最终发现问题在前端。
找到前端的 login.html 代码如下:

// An highlighted block
doLogin() {

					if (this.username == null || this.username == undefined || this.username == '') {
						alert("用户名不能为空");
						return;
					} else if (this.password == null || this.password == undefined || this.password == '') {
						alert("密码不能为空");
						return;
					} else if (this.password.length < 6) {
						alert("密码不能少于6位");
						return;
					}

					var userBO = {
						username: this.username,
						password: this.password
					};
					// console.log(userBO);

					var serverUrl = app.serverUrl;

					var returnUrl = this.returnUrl;
					// form提交
					axios.defaults.withCredentials = false;
					// console.log(axios.defaults);
					axios.post(serverUrl + '/passport/login', userBO)
						.then(res => {
							if (res.data.status == 200) {
								var user = res.data;
								console.log(user);

								window.location.href = app.ctx + "/index.html";
								if (returnUrl != null && returnUrl != undefined && returnUrl != '') {
									window.location.href = returnUrl;
								} else {
									window.location.href = "index.html";
								}

							} else if (res.data.status == 500) {
								alert(res.data.msg);
								return;
							}
						});
				}
			}

登录接口没有问题,存在问题的是跨域访问需要发送cookie时一定要加axios.defaults.withCredentials = true;
修改如下:

// An highlighted block
axios.defaults.withCredentials = true;

如此困扰好久的问题就解决了。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐