AJAX 调用 returns 'undefined'

AJAX call returns 'undefined'

我试图在使用 ajax 调用时将 Date 的值发送到代码后面,但它正在返回。 我在这里做错了什么

 function FetchTime() {
               debugger;
               var Pt_AppDate = document.getElementById("appdate").value;
               var reqs ='{AppointmentDate: "' + Pt_AppDate + '"}';
               $.ajax({
                    type: "POST",
                    url: "AppointmentForm.aspx/FetchATime",
                  data: reqs,
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    success: OnSuccessApptwo,
                    failure: function (response) { alert(response.d); },
                    error: function (response) { alert(response.d); }
               });

          }

背后的代码

public void FetchATime()
          {
               conn.ConnectionString = dbObj.connString;
               conn.Open();
               string query2 = "Select Appointment_time from Appointments where convert(date, Appointment_date) < '" + AppointmentDate + "'";
               SqlCommand cmd2 = new SqlCommand(query2, conn);
               AvailableTime.DataSource = cmd2.ExecuteReader();
               AvailableTime.DataTextField = "Appointment_time";
                DoctorName.DataValueField = "Symptom_id";
               AvailableTime.DataBind();
               AvailableTime.Items.Insert(0, new ListItem("--Select Available Time", "0"));
               conn.Close();
          }

您的客户端代码正在尝试打印 returned 值:

alert(response.d)

但是服务器端代码 没有 return 值:

public void FetchATime()
{
    //...
}

为了return编辑某些东西,你必须return一些东西。 (当然是可序列化的东西。)例如:

public SomeType FetchATime()
{
    //...
    return someObject; // an instance of SomeType
}

然后在您的客户端代码中,您可以访问该对象的属性:

alert(response.someProperty)