Reputation: 3447
I have the following JQuery script :-
<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>
However its always throwing a fail and never hitting my WebMethod. My WebMethod in C# is :-
[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);
}
I cannot seem to figure out why its not hitting my WebMethod.
Any help is very much appreciated!
Thanks for your help and time
******* UPDATE ******************************
$(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);
}
});
};
});
****** SECOND UPDATE **********************
$(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");
}
});
};
});
Upvotes: 1
Views: 4579
Reputation: 1952
ResolveUrl will give you url understood by request
var pageUrl = '<%= ResolveUrl("~/Default.aspx")%>';
$(document).ready(function () {
$("#languageMenu").change(function () {
var value = $("#languageMenu option:selected").val();
setSession(value);
});
setSession('testing');
function setSession(lang) {
alert(lang);
$.ajax({
type: "POST",
url: pageUrl + "/SetUserCulture",
data: "{'lang':'" + JSON.stringify(lang) + "'}",
contentType: "application/json;charset=utf-8;",
dataType: "json",
success: function () {
alert('success');
},
error: function () {
alert("Fail");
}
});
};
});
Upvotes: 0
Reputation: 997
What Dado said - in the ajax call you must specify parameter name.
But you are passing a simple string, don't need JSON for that - just skip
contentType: "application/json;charset=utf-8;",
and use
contentType: "text/html",
Upvotes: 0
Reputation: 17366
Try to write your $.ajax
method call like this:
function setSession(val) {
$.ajax({
type: "POST",
url: "Default.aspx/SetUserCulture",
data: "{'lang':'"+ val + "'}", // Note this portion
contentType: "application/json;charset=utf-8;",
success: function (data) {
alert(data);
__doPostBack('UPMainMenu', '');
__doPostBack('UPContent', '');
},
error: function (e) {
console.log(e);
}
});
}
Because your web method
has lang
parameter passed, so in the ajax call you might have to specify that. though there are many different ways to do that but i have comeup with this solution. I tried to call my web method and it worked.
Note:
If it is a valid json object like {'lang':'hi'}
then jQuery might not send it as json data but instead serialize it to lang=hi
thus you get the error. _Previously it was not passed at all.
Thanks
Upvotes: 1