jQuery Ajax-返回布尔? [英] Jquery Ajax - Return Bool?

查看:78
本文介绍了jQuery Ajax-返回布尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题,试图找出是否已发送电子邮件并显示结果,似乎无法使其正常工作.

newbie question, trying to find out if an email sent, and show result, cant seem to get it to work.

function SendPreview() {
    var value = CKEDITOR.instances['Source'].getData();
    alert(value);
    var model = { EmailBody: value.toString(), EmailTo: $("#SendTo").val(), EmailSubject: $("#Subject").val() };
    var request = $.ajax({
        url: '/Campaign/SendPreviewEmail',
        async: false,
        type: 'POST',
        dataType: 'JSON',
        data: { model: JSON.stringify(model) },
        cache: false,
        success: function (data) {
            if (data) {
                alert("Message Sent");
            } else {
                alert("Message Not Sent, Please check details");
            }
        }
    });
}

[HttpPost]
[ValidateInput(false)]
public bool SendPreviewEmail(string model)
{
    var e = new EmailPreview();
    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);
            return true;
        }
    }
    return false;
}

推荐答案

假定这是ASP.Net MVC,您应该从操作中返回ActionResult(或者至少从它派生出一些内容).下一个问题是,返回true意味着toString()将在bool值上调用,从而导致字符串"True""False".请注意,这两个都等于javascript中的true.而是返回包含结果标志的JSON.

Assuming this is ASP.Net MVC, you should be returning an ActionResult from your action (or at least something that derives from it). The next issue is that returning true will mean toString() will be called on the bool value, resulting in the string "True" or "False". Note that both of these equate to true in javascript. Instead, return JSON containing a result flag.

在jQuery代码中,您还设置了async: false,这确实是一种不好的做法.实际上,如果您检查控制台,则会看到浏览器有关其使用的警告.您应该删除该属性,以便异步发出AJAX请求.您还已在ajax()调用中将dataType设置为JSON,但实际上是在返回一个字符串.尝试以下方法:

In the jQuery code you've also set async: false which is really bad practice to use. In fact, if you check the console you'll see browsers warnings about its use. You should remove that property so that the AJAX request is made asynchronously. You've also set dataType to JSON in the ajax() call, but are actually returning a string. Try this instead:

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 });
}

这篇关于jQuery Ajax-返回布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆