ASP。NET MVC/jQuery/AJAX:链接第一次失败,第二次成功(页面重新加载后)

时间:2021-01-03 03:15:33

Preface

前言

For this question, I have a MVC partial view. The view has a section which displays a list of documents. Each document has a hyperlink: when clicked, the hyperlink takes the user to a second page view displaying additional information.

对于这个问题,我有一个MVC的部分视图。视图有一个显示文档列表的部分。每个文档都有一个超链接:当单击时,超链接将用户带到显示附加信息的第二个页面视图。

The link is inside an unordered list:

链接在一个无序列表中:

<a style="text-decoration:underline;" onclick="sendToDocketSearch('@currentDocument.DktYear','@currentDocument.DktSequence','@currentDocument.DktSubActionID');">@currentDocument.DktYear.ToString().PadLeft(2, '0') - @currentDocument.DktSequence.ToString().PadLeft(5, '0')</a>

When the user clicks the link, it takes them to a sendToDocketSearch javascript function (to prepare to search for the document):

当用户点击链接时,它会将它们带到sendToDocketSearch javascript函数(用于准备搜索文档):

var sendToDocketSearch = function (yearOfDocket, sequenceOfDocket, dktSubActionIDOfDocket) {
        jQuery.ajax({
            type: "POST",
            url: "@Url.Action("DocketSearchOnDemand")",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ docketYear: yearOfDocket, 
                               docketSequence: sequenceOfDocket,
                               DktSubActionID: dktSubActionIDOfDocket,
                             userIsAuthorized: '@Model.userIsAuthorized' }),
            success: function (data) {
                alert(data);
            },
            failure: function (errMsg) {
                alert(errMsg);
            }
        });

        submitForm();
    }

Note that the page/view/form is submitted after the following controller method is run:

请注意,在运行以下控制器方法之后提交页面/视图/表单:

public ActionResult DocketSearchOnDemand(string docketYear, string docketSequence, decimal DktSubActionID, bool userIsAuthorized, PortalIndexView viewmodel)
    {
        System.Web.HttpContext.Current.Session.Add("userIsAuthorized", userIsAuthorized);

        string docketSearch = docketYear + "-" + docketSequence;
        System.Web.HttpContext.Current.Session["DocketSearchOnDemand"] = docketSearch;
        if (DktSubActionID > 0)
        {
            System.Web.HttpContext.Current.Session["DktSubActionID"] = DktSubActionID.ToString();
            System.Web.HttpContext.Current.Session["searchingCustomID"] = true;
        }
        else
        {
            System.Web.HttpContext.Current.Session["DktSubActionID"] = "1";
            System.Web.HttpContext.Current.Session["searchingCustomID"] = false;
        }

        return View(viewmodel);
    }

The above controller method runs; then, because the form is submitted, the HttpPost action for the page takes place. When running it on my local PC, the link is clicked and the next page is loaded without drama.

上述控制器方法运行;然后,由于表单已提交,因此将执行页面的HttpPost操作。当在我的本地PC上运行它时,点击链接并加载下一个页面,没有戏剧效果。

Problem

问题

The problems start when I upload the code to the dev/test server. I don't know how to use breakpoints while troubleshooting an active website, so I follow along with the browser developer tool to monitor network traffic.

当我将代码上传到dev/test服务器时,问题就开始了。我不知道如何在对活动网站进行故障排除时使用断点,所以我使用浏览器开发工具来监视网络流量。

When clicking the link when running the website on my localserver, the process continues:

当在我的localserver上运行网站时点击链接时,该过程继续:

  • the hyperlink takes me to a method where I pass information to be searched
  • 超链接将我带到一个方法,在该方法中我传递要搜索的信息
  • the page/view/form is submitted
  • 页面/视图/提交表单
  • the controller redirects where I have to go.
  • 控制器重定向我要去的地方。

When I click the link on the site and it's on the server, the first click is completely ignored - network traffic shows that it tries to navigate to the controller via the javascript function above, but the failure happens so fast I can't even take a screenshot of it. The page reloads a second time at this point.

当我点击站点上的链接,它在服务器上时,第一次点击完全被忽略了——网络流量显示它试图通过上面的javascript函数导航到控制器,但是失败发生得太快了,我甚至无法对它进行截图。页面此时再次加载。

When I click on the same link a second time, it works without fail.

当我第二次点击相同的链接时,它可以正常工作。

I believe the view/javascript/controller code works because it works the second time (and on subsequent attempts). It just flagrantly fails the first time on the server; after that, the user is fine. I'd like to prevent that "first-time" failure, however, and I'm wondering what the problem could be...

我相信视图/javascript/控制器代码是有效的,因为它是第二次(以及以后的尝试)。它第一次在服务器上就明显地失败了;之后,用户就可以了。我想避免第一次失败,但是,我想知道问题是什么……

  1. Bad timing
  2. 糟糕的时间

I may be passing the information too early (or too late for my website/server to process it properly). The page does it correctly the second time, so maybe I'm just "jumping the gun" by not waiting a little longer for page-loading processes to sort themselves out. (Maybe I can fiddle around with the $(document).ready() javascript portion of the first page to "delay" allowing people to click a link.)

我可能会过早地传递信息(或者对于我的网站/服务器来说,处理得太迟)。页面第二次做对了,所以我可能只是“抢先一步”,不再等待页面加载过程来进行排序。(也许我可以修改第一个页面的$(document).ready() javascript部分以“延迟”允许人们点击链接)。

  1. Code error
  2. 代码错误

I'll be glad to admit bad code if I'm genuinely messing something up. Maybe it's my javascript function, or maybe it's the code in my controller; at any rate, something is making the first pass of that function call be rejected. Maybe my code is bad because the problem doesn't happen the second time, and I'm getting a false sense of security (i.e. there are problems with my code that the system is willing to forgive after the page has thoroughly loaded).

如果我真的把事情搞砸了,我很乐意承认错误的代码。可能是javascript函数,也可能是控制器中的代码;无论如何,某些东西使该函数调用的第一次传递被拒绝。也许我的代码是坏的,因为这个问题第二次没有发生,而且我得到了一个错误的安全意识(例如,我的代码有问题,系统愿意在页面被完全加载之后原谅)。

  1. Server problem/miscellaneous
  2. 服务器问题/杂

I'm wondering if I missed something when I uploaded my latest changes, or if I should have contacted my network team in case there are permissions that need to be activated for the site to work smoothly. I'm already in touch with them regarding something else, so I might take advantage of the opportunity today.

我想知道当我上传最新的更改时是否遗漏了什么,或者如果我应该联系我的网络团队,以防有需要激活的权限才能顺利地工作。我已经和他们联系过其他事情了,所以今天我可能会利用这个机会。

There is an alternative in place that could help me prevent this problem from happening, but I want to find out why the "first-time" failure happens. Other similar actions fail the first time on the site, and I'd like to apply the insights from fixing this issue to them.

有一个替代方案可以帮助我避免这个问题的发生,但是我想找出为什么“第一次”失败会发生。其他类似的行动在网站上第一次失败了,我想把解决这个问题的见解应用到他们身上。

Thank you for looking at this issue. Have a great day.

谢谢您关注这个问题。祝你有美好的一天。

1 个解决方案

#1


1  

Are you sure you want to call submitForm(); before your jQuery.ajax has finished? your ajax call is async so it will hit submitForm(); before it has had time to finish. should submitForm(); be in your success event instead?

您确定要调用submitForm()吗;之前你的jQuery。ajax已完成了吗?您的ajax调用是异步的,因此它将访问submitForm();还没来得及完成。应该submitForm();而是在你的成功事件中?

#1


1  

Are you sure you want to call submitForm(); before your jQuery.ajax has finished? your ajax call is async so it will hit submitForm(); before it has had time to finish. should submitForm(); be in your success event instead?

您确定要调用submitForm()吗;之前你的jQuery。ajax已完成了吗?您的ajax调用是异步的,因此它将访问submitForm();还没来得及完成。应该submitForm();而是在你的成功事件中?