重定向到另一个页面而不发送查询字符串中的任何值
本文关键字:查询 字符串 任何值 另一个 重定向 | 更新日期: 2023-09-27 18:35:48
我正在尝试在用户首次登录时执行用户验证。如果用户尚未经过验证,那么我将代码中的userid
发送到回调方法在回调中,我使用
(window.location.href = "/verifyuser?id=" + response.userId)
但是我不希望这样。 相反,我想对带有值的 URL 进行 ajax 调用。 我不希望用户 ID 在 URL 中可见。我试过使用
$.ajax({
type:"POST"
url:"/verifyuser",
data:{id:response.userId},
async:false
});
但它没有用。我的服务器端代码如下(我正在使用 mvc 4)
[HttpGet]
public ActionResult VerifyUser(string id)
{
ViewBag.Id = id;
return View("~/Views/User/VerifyUser.cshtml");
}
我的JS代码如下:
var postObj = {
url: "Home/Login",
data: {},
callback: function (status, response) {
if (response.status === 400) {
var errors = response.errors;
var serverValidate = new PS.Validate(userObj);
var isvalid = serverValidate.showErrors(errors);
}
else {
if (response.userId !== null && response.userId !== undefined) {
window.location.href = "/verifyuser?id=" + response.userId;
}
else {
window.location.href = "/user-dashboard";
}
}
}
};
me.fm.post(postObj);
有没有办法在有或没有Ajax的情况下做到这一点?谢谢。
如果你不想做一个ajax调用,你可以在你的控制器中返回一个RedirectToAction ActionResult,但你必须确保页面做一个实际的表单发布,而不是使用ajax
如果将 AJAX 请求更改为 POST,则还必须允许控制器操作响应 POST 请求。
[HttpPost]
public ActionResult VerifyUser(string id)
{
ViewBag.Id = id;
return View("~/Views/User/VerifyUser.cshtml");
}