Problem with WebMethod when it is called from Jquery
-
Here is my code:
$('.Status').click(function() {
var divparent = $(this).closest('.tabs-content');
var ruleid = divparent.attr('data-ruleid');
var statusid;
if (divparent.hasClass('inactive')) {
$(this).closest('.tabs-content').removeClass('inactive');
statusid = 1;
}
else {
console.log('active');
$(this).closest('.tabs-content').addClass('inactive');
statusid = 0;
}$.ajax({ type: "POST", Url: "NewRules.aspx/SaveStatus", data: "{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}", contentType: "application/json;charset=utf-8", dataType: "json" }); });
and here is the WebMethod:
[WebMethod]
public static string SaveStatus(string strStatus,string strRuleID)
{
int status = int.Parse(strStatus);
long RuleID;
if (long.TryParse(strRuleID, out RuleID))
{UploadScheduleDisplay display = UpSchDisp.Where(d => d.Id == RuleID).FirstOrDefault() as UploadScheduleDisplay; if (display != null) { //pause or active the display if (display.IsPaused) { display.Resume(); } else { display.Pause(); } //command will delete/add the rule from/to the module CommandUtils.SendNewCommandAudit(CommandTypeEnum.ProgramUploadScheduleRules, 0, unitid, 1, 0, cmd\_changeStatus, WebMethodLogin); } } else { //RuleID is not Valid return ""; } return RuleID.ToString() + " " +status.ToString();
</pre>
The Jquery click works, but it never goes to WebMethod, do you have any idea what is wrong with my code?
Thanks
-
Here is my code:
$('.Status').click(function() {
var divparent = $(this).closest('.tabs-content');
var ruleid = divparent.attr('data-ruleid');
var statusid;
if (divparent.hasClass('inactive')) {
$(this).closest('.tabs-content').removeClass('inactive');
statusid = 1;
}
else {
console.log('active');
$(this).closest('.tabs-content').addClass('inactive');
statusid = 0;
}$.ajax({ type: "POST", Url: "NewRules.aspx/SaveStatus", data: "{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}", contentType: "application/json;charset=utf-8", dataType: "json" }); });
and here is the WebMethod:
[WebMethod]
public static string SaveStatus(string strStatus,string strRuleID)
{
int status = int.Parse(strStatus);
long RuleID;
if (long.TryParse(strRuleID, out RuleID))
{UploadScheduleDisplay display = UpSchDisp.Where(d => d.Id == RuleID).FirstOrDefault() as UploadScheduleDisplay; if (display != null) { //pause or active the display if (display.IsPaused) { display.Resume(); } else { display.Pause(); } //command will delete/add the rule from/to the module CommandUtils.SendNewCommandAudit(CommandTypeEnum.ProgramUploadScheduleRules, 0, unitid, 1, 0, cmd\_changeStatus, WebMethodLogin); } } else { //RuleID is not Valid return ""; } return RuleID.ToString() + " " +status.ToString();
</pre>
The Jquery click works, but it never goes to WebMethod, do you have any idea what is wrong with my code?
Thanks
-
I thought I changed it :) . thank you so much. this usually happens to me when I am working late.
-
I thought I changed it :) . thank you so much. this usually happens to me when I am working late.
-
But with this
$('.Status').click(function() {
var divparent = $(this).closest('.tabs-content');
var ruleid = divparent.attr('data-ruleid');
var statusid;
if (divparent.hasClass('inactive')) {
$(this).closest('.tabs-content').removeClass('inactive');
statusid = 1;
}
else {
console.log('active');
$(this).closest('.tabs-content').addClass('inactive');
statusid = 0;
}$.ajax({ type: "POST", Url: "NewRules.aspx/SaveStatus", data: "{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}", contentType: "application/json;charset=utf-8", dataType: "json" });
This is not still workingggggggg!!!!!!! pLEASE HELP
-
But with this
$('.Status').click(function() {
var divparent = $(this).closest('.tabs-content');
var ruleid = divparent.attr('data-ruleid');
var statusid;
if (divparent.hasClass('inactive')) {
$(this).closest('.tabs-content').removeClass('inactive');
statusid = 1;
}
else {
console.log('active');
$(this).closest('.tabs-content').addClass('inactive');
statusid = 0;
}$.ajax({ type: "POST", Url: "NewRules.aspx/SaveStatus", data: "{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}", contentType: "application/json;charset=utf-8", dataType: "json" });
This is not still workingggggggg!!!!!!! pLEASE HELP
I don't reccomend trying to bind to a class
$('.Status').click(function() {
but to bind to the element ID instead Plus you need to test your bind to make sure it works There are several ways to bind With update panel
$(document).ready(function () {
initiateBinding(); Sys.WebForms.PageRequestManager.getInstance().add\_endRequest(EndRequestHandler);
});
function EndRequestHandler(sender, args) {
initiateBinding();
}function initiateBinding() {
$(document).ajaxStart(function () { }); $(document).ajaxStop(function () { }); $('\[id\*="\_ddl\_RTSR\_SARates\_Unified\_SelectRate"\]').change(function () { alert("I'm Alive"); });
}
with no update panel
$(document).ready(function() {
// Handler for .ready() called.
$('[id*="_ddl_RTSR_SARates_Unified_SelectRate"]').change(function () {
alert("I'm Alive");
});
});So test the bind first, then move on to the function code and report back, I'm leaving in 30 minutes today [edit] test your data,
alert("{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}");
copy the messagebox contents using firefox and paste it into the json validator to make sure it's valid json http://jsonlint.com/[validator^] you have to use slashes to escape out the double quotes
"{\"m_Rate_API_Code\" : \"" + rate_API_Code + "\"}"
Your JSON doesn't look valid to me {"name" : "Jim"}
-
I don't reccomend trying to bind to a class
$('.Status').click(function() {
but to bind to the element ID instead Plus you need to test your bind to make sure it works There are several ways to bind With update panel
$(document).ready(function () {
initiateBinding(); Sys.WebForms.PageRequestManager.getInstance().add\_endRequest(EndRequestHandler);
});
function EndRequestHandler(sender, args) {
initiateBinding();
}function initiateBinding() {
$(document).ajaxStart(function () { }); $(document).ajaxStop(function () { }); $('\[id\*="\_ddl\_RTSR\_SARates\_Unified\_SelectRate"\]').change(function () { alert("I'm Alive"); });
}
with no update panel
$(document).ready(function() {
// Handler for .ready() called.
$('[id*="_ddl_RTSR_SARates_Unified_SelectRate"]').change(function () {
alert("I'm Alive");
});
});So test the bind first, then move on to the function code and report back, I'm leaving in 30 minutes today [edit] test your data,
alert("{strStatus:'" + statusid + "',strRuleID:'" + ruleid + "'}");
copy the messagebox contents using firefox and paste it into the json validator to make sure it's valid json http://jsonlint.com/[validator^] you have to use slashes to escape out the double quotes
"{\"m_Rate_API_Code\" : \"" + rate_API_Code + "\"}"
Your JSON doesn't look valid to me {"name" : "Jim"}
the binding works, the console.log shows that. and the parameters are just numbers, what is wrong with them?> The thing is the page is postback but the webmethod is not running, it only goes to pageload! and One more thing! Sys is not a valid name when I tried to use it!! I don't know why!
-
the binding works, the console.log shows that. and the parameters are just numbers, what is wrong with them?> The thing is the page is postback but the webmethod is not running, it only goes to pageload! and One more thing! Sys is not a valid name when I tried to use it!! I don't know why!
You should call the web method page directly, and see if the debug interface comes up, enter your value and test it. [edit] on the Sys is not a valid name, the Sys exist if the button you binded is inside an update panel. With out the example I provided, you will lose the bind when the panel updates, if you don't have an update panel, then it's just the on ready function. Without seeing the project, it's hard to say why the page is not firing, but most likely it an error in the page name or extension.