Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with WebMethod when it is called from Jquery

Problem with WebMethod when it is called from Jquery

Scheduled Pinned Locked Moved ASP.NET
questionjavascriptjsonhelpcareer
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nafiseh Salmani
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • N Nafiseh Salmani

      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

      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      Url: "NewRules.aspx/PostUser" There is no postUser in the webmethod

      N 1 Reply Last reply
      0
      • J jkirkerx

        Url: "NewRules.aspx/PostUser" There is no postUser in the webmethod

        N Offline
        N Offline
        Nafiseh Salmani
        wrote on last edited by
        #3

        I thought I changed it :) . thank you so much. this usually happens to me when I am working late.

        J 1 Reply Last reply
        0
        • N Nafiseh Salmani

          I thought I changed it :) . thank you so much. this usually happens to me when I am working late.

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          Same here, 2nd set of eyes helps out

          N 1 Reply Last reply
          0
          • J jkirkerx

            Same here, 2nd set of eyes helps out

            N Offline
            N Offline
            Nafiseh Salmani
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • N Nafiseh Salmani

              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

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              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"}

              N 1 Reply Last reply
              0
              • J jkirkerx

                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"}

                N Offline
                N Offline
                Nafiseh Salmani
                wrote on last edited by
                #7

                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!

                J 1 Reply Last reply
                0
                • N Nafiseh Salmani

                  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!

                  J Offline
                  J Offline
                  jkirkerx
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups