AJAX调用返回'undefined'

时间:2017-03-16 17:52:24

标签: c# asp.net ajax code-behind

我正在尝试在使用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();
          }

1 个答案:

答案 0 :(得分:0)

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

alert(response.d)

但是服务器端代码不会返回值:

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

为了返回某些东西,你必须返回一些东西。 (当然可以序列化。)例如:

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

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

alert(response.someProperty)