2017-03-16 125 views
0

我试图发送值的日期代码背后使用ajax调用,但它返回。 我在做什么错在这里AJAX调用返回'undefined'

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(); 
 
      }

回答

0

你的客户端代码试图打印返回值:

alert(response.d) 

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

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

为了东西被退回,你必须返回的东西。 (序列化的东西当然。)例如:

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

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

alert(response.someProperty)