我有以下 JQuery 脚本:-
<script type="text/javascript">
$(document).ready(function () {
$("#languageMenu").change(function () {
var value = $("#languageMenu option:selected").val();
setSession(value);
});
function setSession(val) {
alert(val);
$.ajax({
type: "POST",
url: "Dashboard.aspx/SetUserCulture",
data: JSON.stringify(val),
contentType: "application/json;charset=utf-8;",
success: function () {
__doPostBack('UPMainMenu', '');
__doPostBack('UPContent', '');
},
error: function () {
alert("Fail");
}
});
};
});
</script>
然而,它总是抛出失败并且从不击中我的 WebMethod。我在 C# 中的 WebMethod 是:-
[WebMethod(EnableSession = true)]
public static void SetUserCulture(string lang)
{
HttpContext.Current.Session["CurrentUI"] = lang;
String selectedLanguage = lang;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
我似乎无法弄清楚为什么它没有击中我的 WebMethod。
很感谢任何形式的帮助!
感谢您的帮助和时间
** * ** * *更新* ** * ** * ** * ** * ** * ** * ** * ** * ** * **
$(document).ready(function () {
$("#languageMenu").change(function () {
var value = $("#languageMenu option:selected").val();
setSession(value);
});
function setSession(lang) {
alert(lang);
$.ajax({
type: "POST",
url: "Dashboard.aspx/SetUserCulture",
dataType: "json",
data: "{'lang':'" + lang + "'}",
contentType: "application/json;charset=utf-8;",
success: function (data) {
alert(data);
__doPostBack('UPMainMenu', '');
__doPostBack('UPContent', '');
},
error: function (e) {
console.log(e);
}
});
};
});
** * ***第二次更新** * ** * ** * ** * ** * ** * ****
$(document).ready(function () {
$("#languageMenu").change(function () {
var value = $("#languageMenu option:selected").val();
setSession(value);
});
function setSession(lang) {
alert(lang);
$.ajax({
type: "POST",
url: "Dashboard.aspx/SetUserCulture",
data: "{'lang':'" + JSON.stringify(lang) + "'}",
contentType: "application/json;charset=utf-8;",
dataType: "json",
success: function() {
__doPostBack('UPMainMenu', '');
__doPostBack('UPContent', '');
},
error: function() {
alert("Fail");
}
});
};
});