AJAX - 根据服务器响应的不同做出响应

时间:2014-01-14 09:21:59

标签: c# jquery ajax

这是我的C#代码:

   if (!String.IsNullOrEmpty(bookRoom))
        {
              ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); //If we ever upgrade, its not that important but change this to the right version



        //service.UseDefaultCredentials = true;


        service.Credentials = new WebCredentials("MYNLJ", "", "");
        service.Url = new Uri("http://DIR/EWS/Exchange.asmx"); //This is the EWS file

        Appointment appointment = new Appointment(service);



        String ITMtgMailboxToAccess = roomEmail; //Mailbox name
        FolderId ITMtgCalendarFolderId = new FolderId(WellKnownFolderName.Calendar, ITMtgMailboxToAccess);


        appointment.Subject = "Walk In Meeting";
        appointment.Body = "Test Meeting";



        double htoAdd = Convert.ToDouble(MeetingLength.SelectedItem.Value);


        appointment.Start = DateTime.Now;
        appointment.End = DateTime.Now.AddMinutes(htoAdd);
        CalendarView Checkcv = new CalendarView(appointment.Start, appointment.End); //Don't change this

        try
        {
            FindItemsResults<Appointment> ITMtgfapts = service.FindAppointments(ITMtgCalendarFolderId, Checkcv);

            List<Appointment> ITMtgappointments = new List<Appointment>();
            if (ITMtgfapts.Items.Count > 0) // If there is more than one item
            {

在这里,我想让ajax请求知道预订不成功

   // "Your booking will conflict with another appointment";
            }


            else
            {

让ajax请求知道它是成功的

                        //Success
                       appointment.RequiredAttendees.Add(roomEmail);



                appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);


            }
        }
        catch
        {

        }
    }

AJAX代码:

<script type="text/javascript">
      $(function () {
          $('#BookButton').click(function (event) {
              var form = $('#Form1');
              $.ajax({
                  type: form.attr('method'),
                  url: form.attr('action'),
                  data: $("#BookRoom :input").serialize()
              }).done(function () {
                  // Optionally alert the user of success here...
                  $('#BookRoom').modal('hide');
                  $('#SuccessMsg').text('Meeting Booked');
                  $('#SuccessMessage').modal('show');
                  setTimeout(function () { $('#SuccessMessage').modal('hide'); }, 3000);
              }).fail(function () {
                  // Optionally alert the user of an error here...
                  alert("Error submitting AJAX request");
              });
              event.preventDefault(); // Prevent the form from submitting via the browser.
          });
      });

1 个答案:

答案 0 :(得分:1)

我的建议是回应一个枚举值。优点是可扩展性:

C#

public enum ReponseType : int 
{
  Success: 0,
  InvalidInput: 1,
  ServerError: 2,
  ....
}

的Javascript

var ResponseType = {
  Success: 0,
  InvalidInput: 1,
  ServerError: 2,
  ....
}

在服务器上:

return base.Json(ReponseType.Success);
// or return base.Json(ReponseType.InvalidInput);
// or return base.Json(ReponseType.ServerError);

在客户端:

$.ajax({
  ...
}).done(function (data) {
  if (data === ResponseType.Success) {
    // Notify user: Success
  }
  else if (data === ResponseType.InvalidInput) {
    // Notify user: It is his fault
  }
  else if (data === ResponseType.ServerError) {
    // Notify user: It is your fault
  }
});