2014-01-14 50 views
1

这里是我的C#代码:AJAX - 有不同的反应,取决于服务器的响应

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

+0

呃...?那么你的问题是什么?我真的不喜欢通过你的代码拖网来试图提出一个诚实的问题! – Belogix

+0

@Belogix问题是我如何从C#发回响应给AJAX基本上。 –

回答

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 
    } 
}); 
+0

谢谢,现在就让这个去吧,让我知道我是怎么得到的:) –

+0

Vanillebar Lutz C#代码产生一个错误:期望} –

+0

这个错误位于何处?你能通过pastebin为我提供一个更大的片段吗? – WoIIe