2017-03-16 124 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)