Search code examples
ajaxjsonweb-servicesjsonpasmx

Call web service asmx not working


I'm creating web service asmx and after call through Invoke button work fine.

 Parameter:  id = 1  procedureName = sasatestUpdate

Now I call service through Ajax but nothing happens.

Example:

function callService() {
            $.ajax({
                type: "POST",
                url: "appws.asmx?myServiceUpdate",
                data: "{ idTest: '1', procedureName: 'sasatestUpdate' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { alert("success") },
                failure: function (errMsg) { alert("failure"); }
            });
        }

I do not get any alert (success or failure)...

Web serice

string constring = "Data Source=myDb";
        SqlConnection conn;
        SqlCommand comm;

        [WebMethod]
        public string myServiceUpdate(string id, string procedureName)
        {
            conn = new SqlConnection(constring);
            conn.Open();

            comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.CommandText = procedureName;
            comm.Parameters.AddWithValue("@idTest", id);
            try
            {
                comm.ExecuteNonQuery();
                return "Record Saved";
            }
            catch (Exception)
            {
                return "Not Saved";
            }
            finally
            {
                conn.Close();
            }
        }

Solution

  • There is a mismatch in parameter name of your web method. Change 'idTest' to 'id'.
    

    data: "{ id: '1', procedureName: 'sasatestUpdate' }"