locked
JSON and ASP.NET WebMethod not firing server side method RRS feed

  • Question

  • User1151703306 posted

    Hi there, I need your appreciated help.

    I'm trying to pass parameters to my code behind with WebMethod.

    I'm reaching the success end of ajax but the method in my aspx.cs code behind doesn't get called and I have error.

    Operation failed! Details :'[object Object]

    But on the output I have correctly values :

    alert(JSON.stringify(fasi));

    I'm using a masterpage if it makes a difference.

    How to do resolve this ?

    Please, can you help me ?

    Javascript:

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=imgfasi]").bind("click", function () {
                    var fasi = {};
                    fasi.Txseltlc = $("[id*=txseltlc]").val();
                    fasi.Txrescldisa = $("[id*=txrescldisa]").val();
                    fasi.Ddlauttlc = $("[id*=ddlauttlc]").val();
                    $.ajax({
                        type: "POST",
                        url: "Default.aspx/Savepnfasi",
                        data: '{fasi: ' + JSON.stringify(fasi) + '}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            if (response == "3") {
                                alert("Success!");
                            } else {
                                alert("Operation failed! Details: " + response);
                            }
                        }
                    });
                    return false;
                });
            });
        </script>

    code behind :

        public class pnfasiweb
        {
            public string Txseltlc { get; set; }
            public string Txrescldisa { get; set; }
            public string Ddlauttlc { get; set; }
        }
    
        [WebMethod]
        [ScriptMethod]
        public static void Savepnfasi(pnfasiweb fasi)
        {
            if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
            {         
                string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());
        
                string sql = String.Format(@" UPDATE `dotable` ");
                sql += String.Format(" SET ");
                sql += String.Format(" Aut = ?, ");
                sql += String.Format(" Res = ?, ");
                sql += String.Format(" Dur = ?, ");
                sql += String.Format(" Comp = CASE WHEN Comp IS NULL THEN ? ELSE CONCAT(Comp, '; ', ?) END, ");
                sql += String.Format(" doDateHour = CURRENT_TIMESTAMP() ");
                sql += String.Format(" WHERE ID = ?; ");
        
                using (OdbcConnection cn =
                        new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQLlocalhost"].ConnectionString))
                {
                    using (OdbcCommand command =
                            new OdbcCommand(sql, cn))
                    {
                        try
                        {
                            command.Connection.Open();                       
                            command.Parameters.AddWithValue("param1", fasi.Ddlauttlc.ToString());
                            command.Parameters.AddWithValue("param2", Convert.ToInt32(fasi.Txrescldisa.ToString()));
                            command.Parameters.AddWithValue("param3", Convert.ToInt32(fasi.Txseltlc.ToString()));
                            command.Parameters.AddWithValue("param4", Mpskmt3.Container.TheObjectPropertyName);
                            command.Parameters.AddWithValue("param5", Mpskmt3.Container.TheObjectPropertyName);
                            command.Parameters.AddWithValue("param6", ProductID.ToString());
                            command.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            command.Connection.Close();
                        }
                    }
                }
            }
            else
            {
               //Error
            }
        }

    Wednesday, February 19, 2020 7:30 AM

Answers

  • User314352500 posted

    The problem is here :

    string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());

    because JSON using POST method and I try update query with QueryString (GET) method ... please can you help me ? 

    Try this :

    var qString = "?" + window.location.href.split("?")[1];
    var fasi = {};
    
    ...
    
    url: "Default.aspx/Savepnfasi" + qString,
    data: '{fasi: ' + JSON.stringify(fasi) + '}',

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2020 2:58 PM

All replies

  • User1151703306 posted

    The problem is here :

    string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());

    because JSON using POST method and I try update query with QueryString (GET) method ... please can you help me ? 

    Wednesday, February 19, 2020 11:27 AM
  • User314352500 posted

    The problem is here :

    string ProductID = Mpskmt3.Base64ForUrlDecode(HttpContext.Current.Request.QueryString["id"].ToString());

    because JSON using POST method and I try update query with QueryString (GET) method ... please can you help me ? 

    Try this :

    var qString = "?" + window.location.href.split("?")[1];
    var fasi = {};
    
    ...
    
    url: "Default.aspx/Savepnfasi" + qString,
    data: '{fasi: ' + JSON.stringify(fasi) + '}',

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 19, 2020 2:58 PM