简体   繁体   English

将参数传递给 window.location.href 失败

[英]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.

相关问题 window.location.href上的第二个参数在Controller上为NULL - Second parameter on window.location.href gets NULL on Controller Blazor 替代 JS window.location.href - Blazor alternative to JS window.location.href window.location.href将HttpContext.Current.Request.Path返回为“ /” - window.location.href returns HttpContext.Current.Request.Path as“/” window.location.href 在“&amp;”字符后不返回任何内容 - window.location.href not returning anything after the “&” character 如何使用 window.location.href 从基于 ASPX 的视图将参数传递给 Action Method - How to pass params to Action Method from ASPX based view using window.location.href asp.net OnClientClick window.location.href在谷歌浏览器中不起作用 - asp.net OnClientClick window.location.href not working in google chrome 通过按此按钮,不要在js window.location.href中使用此命令移动到所需页面 - By pressing the button do not move to the desired page with this command in js window.location.href 使用 POST 请求替代 window.location.href - ASP.NET MVC - Alternative to window.location.href using POST request - ASP.NET MVC 从 EventListener function 调用时,Javascript 重定向(window.location.href)在 Asp.NET 核心项目中不起作用 - Javascript redirect (window.location.href) doesn't work in an Asp.NET Core project when called from an EventListener function javascript:window.location.href在Firefox中打开新标签,并在IE中打开新窗口(而不是弹出窗口)。 我希望页面在同一窗口中打开 - javascript:window.location.href opens new tab in firefox and new window (rather as a pop up ) in IE . I want the page to open in the same window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM