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
@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;
in the loggedUser
variable and using it as the input paramter in the window.location.href = url "?custEmail=" loggedUser;
. It throws error like 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. I even tried to like below
var loggedUser = User.Identity.Name;
It even errors out like Uncaught ReferenceError: User is not defined
Can anyone suggest what is that I am missing here
CodePudding user response:
Please add quotation marks like below:
var loggedUser = "@User.Identity.Name"; //or `@User.Identity.Name`