jquery ajax仅适用于IE
我有一个双语 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的问题(后端可能还有其他问题)是您的 AJAX 请求可能无法在其他/较新的浏览器中完成。当您这样调用时:
...您告诉浏览器导航离开,这意味着它会立即去那里,无论之前的 AJAX 请求是否完成(浏览器可能会提前终止该请求并继续前行)。
相反,您应该在该请求完成后重定向,并在您的成功处理程序中执行重定向,如下所示:
这样您只告诉用户在请求完成后更改页面您已成功提出更改语言的请求。
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:
...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:This way you're only telling the user to change pages after you've successfully made a request to change the language.