【发布时间】:2019-03-30 09:17:09
【问题描述】:
我知道同样的问题有答案,但它们在我的项目中不起作用。 如果我向员工发送消息,我有控制器。 id 我用 ajax。 我从 db 收到的电子邮件。但是 getEmployeeEmail() 将我的电子邮件返回给我(没错) 控制器名称:EmployersActivity
当我发送帖子时,代码不起作用。 我的ajax邮编:
$(document).ready(function () {
$(".buttonSendEmail").click(function () {
var orderText = $(".orderData").text();
alert(orderText);
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
url: "@(Url.Action("Create", "EmployersActivity"))",
data: { id: 1 },
dataType: "json",
traditional: true,
error: function (message) {
alert("error on start")
$(".contentReqGood").show();
redirect();
},
success: function (result) {
if (result.status === 1) {
alert("error")
} else {
$(".contentReqGood").show();
redirect();}})})});
asp.net mvc 代码:
[HttpGet]
[Authorize]
public ActionResult Create()
{
return View();
}
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Create(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var email = db.employees.Find(id);
if (email == null)
return HttpNotFound();
if (email != null)
{
if (db.SaveChanges() == 1)
{
string mailTemplate;
string title = "asdasd";
string preview = "asdasdasd";
var sr = new StreamReader(Server.MapPath("~/App_Data/Templates/" + "InfoEmail.txt"));
mailTemplate = sr.ReadToEnd();
string messageBody = string.Format(mailTemplate, title, preview);
new MailSender
{
Sender = "news@omegasoftware.eu",
Recipient = "news@omegasoftware.eu",
RecipientsBcc = getEmployeeEmail(),
Subject = title,
Body = messageBody
}.Send();}}
return View();}
【问题讨论】:
-
删除
contentType: 'application/json; charset=utf-8',选项(或者您需要对数据进行字符串化 -data: JSON.stringify({ id: 1 }),)。您可以删除无意义的traditional: true,选项(您不是发送简单的数组)。然后你需要删除dataType: "json",(你没有返回json)
标签: javascript asp.net .net ajax asp.net-mvc