我是.NET菜鸟,正在使用.NET Core 2.2
和Visual Studio 2019 Community Edition
和sendgrid-csharp
NuGet软件包
我有一个通过ajax
调用发送到控制器方法的表格,该方法通过SendGrid
发送电子邮件。电子邮件已成功发送,我可以记录响应。但是,响应永远不会返回到ajax
调用,而只会挂在浏览器中直到超时。
我想将response.StatusCode
响应中的SendGrid
返回到ajax
调用。
HomeController.cs
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using SendGrid;
using SendGrid.Helpers.Mail;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Identity.UI.Services;
namespace MyProject.Controllers {
public class HomeController:Controller {
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment) {
_hostingEnvironment = hostingEnvironment;
}
[HttpPost]
public async Task<JsonResult> SendMessageEmail() {
using (var reader = new System.IO.StreamReader(Request.Body)) {
var formData = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());
formData["subject"] = "Email Subject";
formData["body"] = "Email Body";
EmailSender emailSenderService = new EmailSender();
var response = await emailSenderService.Execute(formData["fromemail"], formData["toemail"], formData["subject"], formData["body"], formData["body"]);
Console.WriteLine("-----------------");
Console.WriteLine("response: ", response);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers);
Console.ReadLine();
Console.WriteLine("-----------------");
return new JsonResult(response.StatusCode);
}
}
}
internal class EmailSender {
public async Task<Response> Execute(string From, string To, string subject, string plainTextContent, string htmlContent) {
var apiKey = "MY_SEND_GRID_KEY";
var client = new SendGridClient(apiKey);
var from = new EmailAddress(From);
var to = new EmailAddress(To);
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
return response;
}
}
}
form.js
$(document).on('submit', 'form#email-message-form', function(event) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
var emailMessageForm = $('form#email-message-form');
var messageTo = $('form#email-message-form #message-to');
var messageFrom = $('form#email-message-form #message-from');
var data = {};
data.toemail = messageTo.val()
data.fromemail = messageFrom.val()
var request = $.ajax({
url: "/Home/SendMessageEmail",
method: "POST",
data: JSON.stringify(data),
contentType: 'application/json',
dataType: "json",
cache: false,
async: false
}).done(function (msg) {
console.log('Success: ', msg);
}).fail(function (jqXHR, textStatus) {
console.log("Send email method failed: " + textStatus);
});
});
答案 0 :(得分:0)
SIGH –显然我是个白痴,我要做的就是将我的async: false
文件中的async: true
更改为form.js
。
已更新并正在工作的form.js
$(document).on('submit', 'form#email-message-form', function(event) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
var emailMessageForm = $('form#email-message-form');
var messageTo = $('form#email-message-form #message-to');
var messageFrom = $('form#email-message-form #message-from');
var data = {};
data.toemail = messageTo.val()
data.fromemail = messageFrom.val()
var request = $.ajax({
url: "/Home/SendMessageEmail",
method: "POST",
data: JSON.stringify(data),
contentType: 'application/json',
dataType: "json",
cache: false,
async: true
}).done(function (msg) {
console.log('Success: ', msg);
}).fail(function (jqXHR, textStatus) {
console.log("Send email method failed: " + textStatus);
});
});