Reputation: 1686
hi guys sorry i m really new to mvc3 javascript jquery etc. i have an internal server error 500
this is the controller:
[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});
}
}
and this is the way in which i call the controller:
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);
}
});
If you read the code the controller just send an email and IT WORK FINE THERE are no exception so why ajax say there is a 500 internal server error?
Upvotes: 1
Views: 2278
Reputation: 139748
By default GET
requests are not allowed on JsonResult
so you need to excplicitly allow them, with setting the JsonRequestBehavior
property:
return Json(
new { text = "Everything is fine " + userEmail, risultato = true },
JsonRequestBehavior.AllowGet
);
Or use POST as your request method in your AJAX call.
Upvotes: 3