在javascript ajax中从Webmethod和Alert返回值

时间:2019-06-25 01:41:26

标签: javascript c# ajax webmethod

我试图在来自WebMethod的javascript ajax中提醒numberOfHours。 我不确定如何从WebMethod检索值并将其解析为ajax以获得警报消息。

下面是我正在使用的webmethod和ajax:

// WebMethod aspx.cs

[System.Web.Services.WebMethod]
    public static string GetFirstDateAndLastDate(DateTime firstDayDate, DateTime lastDayDate,string ddlName)
    {
        //String daresult = null;
        string firstDay = firstDayDate.ToString("yyyy/MM/dd");
        string lastDay = lastDayDate.ToString("yyyy/MM/dd");

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
        //testing purpose only
        SqlCommand com = new SqlCommand("SELECT sum([NumberOfHours]) FROM [LoginDB1].[dbo].[tbOT] where [Date] between '" + firstDay + "' And  '" + lastDay + "' AND name =  + '" + ddlName + "'", conn);
        //say for example numberOfhours get  "9.0" hours from sql

        SqlDataAdapter sqlDa = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        sqlDa.Fill(dt);

        //trying with arraylist but not able to return
       // ArrayList rows = new ArrayList();           
        //foreach (DataRow dataRow in dt.Rows)
       // rows.Add(string.Join(";", dataRow.ItemArray.Select(item => 
        item.ToString())));


        return "OK";
    } 

//脚本aspx

                 var selectedDate = $("[id$=datepicker]").datepicker('getDate');

                            var firstDay = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), 1);
                            var lastDay = new Date(selectedDate.getFullYear(), selectedDate.getMonth() + 1, 0);                 

                            var data = {};
                            data.firstDayDate =firstDay.getFullYear() + "-" + (firstDay.getMonth() + 1) + "-" + firstDay.getDate();      
                            data.lastDayDate = lastDay.getFullYear() + "-" + (lastDay.getMonth() + 1) + "-" + lastDay.getDate();                          
                            data.ddlName = $("[id$=ddlName] option:selected").text();
                            console.log("Log Data", data)
                            $.ajax({
                                type: "POST",
                                url: "SupervisorOTRequest.aspx/GetFirstDateAndLastDate",                                  
                                data: JSON.stringify(data),
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",                                   

                                     success: function (response) {

                                    if (response.d == "OK") {

                                          //alert return value                  

                                    }                                   

                            });

请提前告知并感谢。

0 个答案:

没有答案