jquery ajax仅适用于IE

发布于 2024-12-09 04:16:16 字数 1791 浏览 0 评论 0原文

我有一个双语 MVC 3 应用程序。我使用下拉列表通过将下拉列表的值保存到 cookie 和会话中来更改语言。问题是当我发布它时,它只能在 IE 中运行。以下是我的代码。注意:该网站将重新加载,当我关闭 Firefox 或 chrome 并重新响应时,语言已更改,但如果我不关闭 FF 或 chrome,则什么也不会发生。非常感谢您的帮助。 我使用过 $.get、$.post 各种组合。

JavaScript 代码:

$(function () {
    $('#languagesDiv select').change(function () {
        var myvalue = $(this).val();
        $.ajax({
            type: "POST",
            dataType: "xml",
            url: "/Language/SetLanguage",
            data: { code: myvalue },
            success: function (data) {
            },
            error: function (xhr, textStatus, errorThrown) {
            }
        });

        //             @*   $.post('@Url.Action("SetLanguage", "Language")', { code: $(this).val() },
        //                            function (result) {
        //                            }
        //                );*@

        var myDate = new Date();
        myDate.setDate(myDate.getDate() + 21);

        $.cookie('MyData', $(this).val(), { path: '/', expires: myDate });

        //window.location.reload();
        window.location.href = '/News/Index';
    });
}); 

C# 代码:

//tested with and without [httppost]
public void SetLanguage(string code)
{
    if (Session["MyCulture"] != null && Convert.ToString(Session["MyCulture"]) != code ) 
    {
        Session["MyCulture"] = code;
        HttpCookie aCookie = Request.Cookies["MyData"]; // new HttpCookie("MyData");
        aCookie.Value = code;
        //HttpCookie aCookie = Request.Cookies["LangCookie"];
        aCookie.Expires = System.DateTime.Now.AddDays(21);
        Response.Cookies.Add(aCookie);
        //Response.AppendCookie(aCookie);
    }

    //return RedirectToAction("Index", "News");           
}

再次感谢您。

I have a bilangual MVC 3 application. I use a dropdownlist to change the language by saving the value of the dropdownlist into cookie and session. the problem is when I release it, it works only in IE. following is my code. note: the site will be reloaded and when I close firefox or chrome and reponed it, the language has been changed but nothing happend if I dont close FF or chrome. thank you very much for your help.
I have used $.get, $.post every kind of combinations.

JavaScript code:

$(function () {
    $('#languagesDiv select').change(function () {
        var myvalue = $(this).val();
        $.ajax({
            type: "POST",
            dataType: "xml",
            url: "/Language/SetLanguage",
            data: { code: myvalue },
            success: function (data) {
            },
            error: function (xhr, textStatus, errorThrown) {
            }
        });

        //             @*   $.post('@Url.Action("SetLanguage", "Language")', { code: $(this).val() },
        //                            function (result) {
        //                            }
        //                );*@

        var myDate = new Date();
        myDate.setDate(myDate.getDate() + 21);

        $.cookie('MyData', $(this).val(), { path: '/', expires: myDate });

        //window.location.reload();
        window.location.href = '/News/Index';
    });
}); 

C# code:

//tested with and without [httppost]
public void SetLanguage(string code)
{
    if (Session["MyCulture"] != null && Convert.ToString(Session["MyCulture"]) != code ) 
    {
        Session["MyCulture"] = code;
        HttpCookie aCookie = Request.Cookies["MyData"]; // new HttpCookie("MyData");
        aCookie.Value = code;
        //HttpCookie aCookie = Request.Cookies["LangCookie"];
        aCookie.Expires = System.DateTime.Now.AddDays(21);
        Response.Cookies.Add(aCookie);
        //Response.AppendCookie(aCookie);
    }

    //return RedirectToAction("Index", "News");           
}

Thank you again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-12-16 04:16:17

我看到的问题(后端可能还有其他问题)是您的 AJAX 请求可能无法在其他/较新的浏览器中完成。当您这样调用时:

window.location.href = '/News/Index';

...您告诉浏览器导航离开,这意味着它会立即去那里,无论之前的 AJAX 请求是否完成(浏览器可能会提前终止该请求并继续前行)。

相反,您应该在该请求完成后重定向,并在您的成功处理程序中执行重定向,如下所示:

$(function () {
  $('#languagesDiv select').change(function () {
    var myvalue = $(this).val();
    $.ajax({
      type: "POST",
      dataType: "xml",
      url: "/Language/SetLanguage",
      data: { code: myvalue },
      success: function (data) {
        var myDate = new Date();
        myDate.setDate(myDate.getDate() + 21);    
        $.cookie('MyData', $(this).val(), { path: '/', expires: myDate });

        window.location.href = '/News/Index';
      }
    });
  });
}); 

这样您只告诉用户在请求完成后更改页面您已成功提出更改语言的请求。

The problem I see (there could be others with the backend) is that your AJAX request likely won't complete in the other/newer browsers. When you call this:

window.location.href = '/News/Index';

...you're telling the browser to navigate away, this means it's instantly going there, regardless of whether that previous AJAX request completed (the browser will likely kill the request early and move on).

Instead, you should redirect after that request completes and do the re-direct in your success handler, like this:

$(function () {
  $('#languagesDiv select').change(function () {
    var myvalue = $(this).val();
    $.ajax({
      type: "POST",
      dataType: "xml",
      url: "/Language/SetLanguage",
      data: { code: myvalue },
      success: function (data) {
        var myDate = new Date();
        myDate.setDate(myDate.getDate() + 21);    
        $.cookie('MyData', $(this).val(), { path: '/', expires: myDate });

        window.location.href = '/News/Index';
      }
    });
  });
}); 

This way you're only telling the user to change pages after you've successfully made a request to change the language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文