2012-07-12 52 views
1

嗨,大家好抱歉,我真的新的MVC3的JavaScript的jQuery等 我有一個內部服務器錯誤500MVC 3 asp.net 500內部服務器錯誤

這是控制器:

[HttpGet] 
    public JsonResult GetEmail(string title, string notes) 
    { 
     byte[] pdf = null; 
     byte[] excel = null; 
     string userEmail = ""; 
     try 
     { 
      pdf = GetFileForMail("PDF", "ServiceArea_" + System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".pdf", title, notes); 
      excel = GetFileForMail("EXCEL", "ServiceArea_" + System.DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xls", title, notes); 



      MembershipUser mu = Membership.GetUser(this.MembershipData.Principal.Identity.Name); 
      userEmail = mu.Email.ToString(); 


      System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(userEmail, 
                     "[email protected]", 
                     title, 
                     notes); 
      mailMsg.To.Add("[email protected]"); 
      mailMsg.IsBodyHtml = true; 

      string mese = ""; 
      string giorno = ""; 
      string ore = ""; 

      if (DateTime.Now.Month < 10) 
       mese = "0" + DateTime.Now.Month; 
      else 
       mese = "" + DateTime.Now.Month; 

      if (DateTime.Now.Day < 10) 
       giorno = "0" + DateTime.Now.Day; 
      else 
       giorno = "" + DateTime.Now.Day; 

      if(DateTime.Now.Hour < 10) 
       ore = "0" + DateTime.Now.Hour; 
      else 
       ore = "" + DateTime.Now.Hour; 

      System.Net.Mail.Attachment att = new System.Net.Mail.Attachment(new MemoryStream(pdf), DateTime.Now.Year + mese + giorno + "_" + ore + DateTime.Now.Minute + " Report.pdf", System.Net.Mime.MediaTypeNames.Application.Pdf); 

      System.Net.Mail.Attachment att2 = new System.Net.Mail.Attachment(new MemoryStream(excel), DateTime.Now.Year + mese + giorno + "_" + ore + DateTime.Now.Minute + " Report.xls", "application/vnd.ms-excel"); 

      mailMsg.Attachments.Add(att); 
      mailMsg.Attachments.Add(att2); 
      System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient(); 
      sc.Host = "192.168.99.1"; 
      sc.Send(mailMsg); 
      return Json(new { text = "Everything is fine " + userEmail, risultato = true }); 
     } 
     catch (Exception e) { 
      return Json(new { text = "Unexpected error" + userEmail , risultato = false}); 
     } 


    } 

這是我稱之爲控制方式:

jQuery.ajax({ 
       type: "GET", 
       url: options.EmailUrl, 
       dataType: "json", 
       data: 
       { 
        title: viewModel.saReportTitle(), 
        notes: viewModel.saReportNotes() 
       }, 
       success: function (data, textStatus, jqXHR) { 
        jQuery("#sa-dialog-alert").dialog('open'); 
        jQuery("#sa-dialog-alert").dialog('option', 'title', 'Invio Mail Eseguito'); 
        jQuery("#sa-dialog-alert").text(data.text); 
       } 
       , 
       error: function (data, textStatus, errorThrown) { 
        jQuery("#sa-dialog-alert").dialog('open'); 
        jQuery("#sa-dialog-alert").dialog('option', 'title', 'Errore'); 
        jQuery("#sa-dialog-alert").text("Errore nell'invio mail: " + errorThrown); 
       } 
      }); 

如果你讀碼器只是發送一封電子郵件 和IT做工精細沒有excep爲什麼ajax說有500內部服務器錯誤?

+2

你怎麼知道沒有例外?您正吞嚥異常,返回JSON結果。 – Oded 2012-07-12 18:11:27

+0

@ user448381您是否意味着電子郵件發送正常,但您在AJAX請求中遇到異常? – formatc 2012-07-12 18:12:58

+0

是的,電子郵件發送罰款,但程序發生錯誤後:function(data,textStatus,errorThrown)jQuery(「#sa-dialog-alert」)。dialog('open'); jQuery(「#sa-dialog-alert」)。dialog('option','title','Errore'); jQuery(「#sa-dialog-alert」)。text(「Errore nell'invio mail:」+ errorThrown); – 2012-07-12 18:14:44

回答

3

默認情況下GET請求不允許JsonResult所以你需要excplicitly讓他們,設置JsonRequestBehavior屬性:

return Json(
    new { text = "Everything is fine " + userEmail, risultato = true }, 
    JsonRequestBehavior.AllowGet 
); 

或者使用POST在你的AJAX調用的請求方法。

+0

簡單,完全錯過了它。 – formatc 2012-07-12 18:19:35

+0

好的,我檢查這個 – 2012-07-12 18:20:11

+0

OMG MAN YOU SOOOOOOOOO GREAT – 2012-07-12 18:21:58