首页 > 网站 > WEB开发 > 正文

js页面跳转,url带url参数解决方案

2024-04-27 14:09:27
字体:
来源:转载
供稿:网友

js页面跳转,url带url参数解决方案

今天,在做一个项目的时候,向后端发送了一个Ajax请求,后端返回了一个字符串,告诉我未登录。那么我需要跳转到登录页面,同时告诉登录页面,登录成功后,需要跳回的url。也就是标题所说,url中的一个参数为url.

例:

http://localhost:8080/User/Login?returnUrl=http://localhost:8080/PRoduct/index?id=123&attr=456

假如我们要直接使用

window.location.href = "http://localhost:8080/User/Login?returnUrl=http://localhost:8080/Product/index?id=123&attr=456"

这种方式,那么attr=456将做为user/login的参数,而非product/index的。故需要对returnUrl的值进行编码。

var returnUrl = encodeURIComponent("http://localhost:8080/Product/index?id=123&attr=456");window.location.href = "http://localhost:8080/User/Login?returnUrl" + returnUrl ;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表