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;
}
ajax({ async: true, contentType: 'application/json; charset=utf-8', type: "POST", dataType: 'json', data: JSON. stringify(arrays), url: "MyHandler. ashx", success: function (result) { b = true; }, error: function () { alert('Error occurred'); } });
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.
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 });
}
Actually return
doesn't send anything back to the browser, you have to write data to be sent back to the browser, probably Response.Write
, not a familiar with this.
Also, on the client side
if (data)
is same for any data, it'll evaluate to true if any data sent back to the browser, so need to check actual data, could be something like this
if (data == 1)
Or, for json, it could be
if (data.success) // if you send a json response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With