jquery - 单击 Razor Pages Asp 发送电子邮件和重定向
问题描述
正如标题所暗示的那样,我很难通过单击发布和重定向
以下是发送电子邮件并重定向到新页面的表单。我知道问题出在重定向部分,好像删除重定向一样,电子邮件就会发送。
<div class="form-actions no-color">
<p>
From : <input type="text" asp-for="email.From" value="email" /><br />
To : <input type="text" asp-for="email.To" value="@principal.EmailAddress" /><br />
Subject : <input type="text" asp-for="email.Subject" /><br />
Body : <textarea asp-for="email.Body"></textarea><br />
<a asp-page="./Edit" asp-route-id="@item.Id" class="btn btn-md float-end text-white fw-bold mt-3 mb-2 grow" id="emailBtn" style="background-color: #1da1f2">
</a>
</p>
</div>
</form>
我在下面使用的 Jquery
$(function () {
$("#emailBtn").click(function () {
$("#emailForm").submit()
var dataString = $(this).serialize();
e.preventDefault()
$.ajax({
type: "POST",
url: "?handler=SendEmail",
data: dataString,
success: function () {
}
})
});
})
和我的服务器端代码
public async Task OnPostSendEmail()
{
using (var smtp = new System.Net.Mail.SmtpClient("stp"))
{
var emailMessage = new MailMessage();
emailMessage.From = new MailAddress("email");
emailMessage.To.Add(email.To);
emailMessage.Subject = email.Subject;
emailMessage.Body = email.Body;
await smtp.SendMailAsync(emailMessage);
}
}
解决方案
如果你想在ajax之后重定向,你可以使用。window.location.href
这是一个演示:
形式:
<form id="emailForm" method="post">
<div class="form-actions no-color">
<p>
From : <input type="text" asp-for="email.From" value="email" /><br />
To : <input type="text" asp-for="email.To" value="@principal.EmailAddress" /><br />
Subject : <input type="text" asp-for="email.Subject" /><br />
Body : <textarea asp-for="email.Body"></textarea><br />
<a asp-page="./Edit" asp-route-id="@item.Id" class="btn btn-md float-end text-white fw-bold mt-3 mb-2 grow" id="emailBtn" style="background-color: #1da1f2">
</a>
</p>
</div>
</form>
js:
$("#emailBtn").click(function () {
var dataString = $("#emailForm").serialize();
$.ajax({
type: "POST",
url: "?handler=SendEmail",
data: dataString,
}).done(function () {
window.location.href = $("#emailBtn").attr("href");
}
)
});
推荐阅读
- laravel - 将验证错误从 Laravel 控制器发送回 Vue 组件的正确方法是什么?
- ios - iOS应用程序空白navigationView在某些设备模拟器上打开屏幕,而不是在其他设备模拟器上
- java - Java欧拉数结果无穷大
- python - 币安 API 问题 - Python
- sql - 在 sql access 中为每个产品选择前 2 行
- react-native - react-native-ssl-pinning - 此服务器的证书无效
- javascript - Dreamweaver 实时预览不使用 CSS 样式表和其他参考
- docker - 连接到上游 Nginx Docker 时连接被拒绝
- javascript - 如何使用 express 显示来自多个不同目录的网页
- ocr - tesseract OCR 无法识别这些数字