假设这是 ASP.Net MVC,您应该返回一个ActionResult
来自你的行为(或者至少是由它衍生的东西)。下一个问题是返回true
将意味着toString()
将被称为bool
值,产生字符串"True"
or "False"
。请注意,这两者都等于true
在 JavaScript 中。相反,返回包含结果标志的 JSON。
在 jQuery 代码中你还设置了async: false
这确实是一种不好的做法。事实上,如果您检查控制台,您会看到浏览器关于其使用的警告。您应该删除该属性,以便异步发出 AJAX 请求。您还设置了dataType
to JSON
in the ajax()
调用,但实际上返回一个字符串。试试这个:
function SendPreview() {
var value = CKEDITOR.instances['Source'].getData();
var model = { EmailBody: value.toString(), EmailTo: $("#SendTo").val(), EmailSubject: $("#Subject").val() };
var request = $.ajax({
url: '/Campaign/SendPreviewEmail',
type: 'POST',
dataType: 'JSON',
data: { model: JSON.stringify(model) },
cache: false,
success: function (data) {
if (data.emailSent) { // note the object parameter has changed
alert("Message Sent");
} else {
alert("Message Not Sent, Please check details");
}
}
});
}
[HttpPost]
[ValidateInput(false)]
public ActionResult SendPreviewEmail(string model)
{
var e = new EmailPreview();
var result = false;
JavaScriptSerializer objJavascript = new JavaScriptSerializer();
e = objJavascript.Deserialize<EmailPreview>(model);
if (!string.IsNullOrEmpty(e.EmailTo) && !string.IsNullOrEmpty(e.EmailSubject) && !string.IsNullOrEmpty(e.EmailBody))
{
if (IsValidEmail(e.EmailTo))
{
_mailService.SendMail(account.Email, e.EmailTo, e.EmailSubject, e.EmailBody, true);
result = true;
}
}
return Json(new { emailSent = result });
}