简体   繁体   中英

trying to send value from front end javascript to backend c# using ajax call,but the value is getting NULL in backend,i don't know where i did mistake

Expecting

What will be the value in the textbox that value should be sent to the backend? I think the issue with the call, but I don't where it is

Javascript

function employeeLeavesList() {
                        var emp = {}
                        emp.EMPID = $("#employid").val();
                        alert(emp.EMPID);
                        $.ajax({
                            type:'POST',
                            url: 'service.asmx/employee_leaveslist',
                            data: JSON.stringify(emp),
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (data) {}
                        });

                    }

class.cs

public class emp
{
    public string EMPID { get; set; }
}

webmethod.asmx.cs

[WebMethod]
        public void employee_leaveslist()
        {
            emp emp = new emp();
            List<employee_leave_list> list = new List<employee_leave_list>();
            SqlConnection connection = new SqlConnection("Data Source = Champ; Initial Catalog = sample; Integrated Security = True");
            SqlCommand cmd = new SqlCommand("select emp_id,leaves_form, leaves_upto, leave_type, description, status, no_of_leaves from leaverequest where emp_id = '" + emp.EMPID + "' ", connection);
            cmd.CommandType = CommandType.Text;
            connection.Open();
            SqlDataReader idr = cmd.ExecuteReader();
            connection.Close();

        }

First, modify the 'data' parameter of your $.ajax() call as below,

data: { empid : $('#employid').val() },

Then, modify the first line of webmethod employee_leaveslist(),

emp emp = new emp(){ EMPID  = (string)Request.Form["empid"] };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM