[英]Passing parameter to window.location.href fails
I am trying to pass the currently logged in Azure AD user identity into the ajax method so it can be used as the input parameter in the window.location.href
我试图将当前登录的 Azure AD 用户身份传递给 ajax 方法,以便它可以用作
window.location.href
的输入参数
@section Scripts {
<script type="text/javascript">
$("#button").click(function () {
var orderedQuantity = $("#txt").val();
var orderId = $("#orderId").val();
var data = {
orderId: orderId,
orderedQuantity: orderedQuantity,
}
var loggedUser = @User.Identity.Name;
$.ajax({
type: 'POST',
url: '@Url.Action("EditItem", "Orders")',
data: data,
dataType: "json",
success: function (result) {
if (result.status === "NotAvailable") {
$("#errorMessage").val("Enter a valid Quantity");
}
else {
var url = '@Url.Action("Index", "Orders")';
window.location.href = url + "?custEmail="+loggedUser;
}
},
error: function (error) {
alert(error);
}
});
});
</script>
I am storing the @User.Identity.Name;
我正在存储
@User.Identity.Name;
in the loggedUser
variable and using it as the input paramter in the window.location.href = url + "?custEmail="+loggedUser;
在
loggedUser
变量中并将其用作window.location.href = url + "?custEmail="+loggedUser;
的输入参数window.location.href = url + "?custEmail="+loggedUser;
. . It throws error like
Uncaught SyntaxError: Invalid or unexpected token
它抛出错误,如
Uncaught SyntaxError: Invalid or unexpected token
Can anyone say if I am missing anything here.. I am passing the input paramter to the Orders page like this in other places and it fails in ajax method.任何人都可以说我是否在这里遗漏了任何东西。我正在将输入参数传递给其他地方的订单页面,但它在 ajax 方法中失败。 I even tried to like below
我什至试着喜欢下面
var loggedUser = User.Identity.Name;
It even errors out like Uncaught ReferenceError: User is not defined
它甚至会出错,比如
Uncaught ReferenceError: User is not defined
Can anyone suggest what is that I am missing here谁能建议我在这里缺少什么
请添加引号,如下所示:
var loggedUser = "@User.Identity.Name"; //or `@User.Identity.Name`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.