1

嗨,对不起,我对 mvc3 javascript jquery 等真的很陌生。我有一个内部服务器错误 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,
                                                                        "mailexample@mail.com",
                                                                        title,
                                                                        notes);
            mailMsg.To.Add("no-replay@valuelab.it");
            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);
                }
            });

如果您阅读了代码,控制器只需发送一封电子邮件,它就可以正常工作,没有例外,那么为什么 ajax 会说存在 500 内部服务器错误?

4

1 回答 1

3

默认情况下GET不允许请求,JsonResult因此您需要通过设置JsonRequestBehavior属性来明确允许它们:

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

或者在您的 AJAX 调用中使用 POST 作为您的请求方法。

于 2012-07-12T18:18:31.373 回答