0

can anybody help me with this topic?

I'm doing an ajax post call and I receive status 200 OK but error handler is fired. I don't know if I'm sending wrong data to server or something like that.

Here is the code:

$('#btn_sendMail').on('click', function (ev) {
    ev.preventDefault();
    var sub = $('#subject').val();
    var body = $('#message').val();
    var displayNameMailAddress = $('#name').val() + " - " + $('#email').val();

    var formData = new FormData();
    formData.append('subject', sub);
    formData.append('body', body);
    formData.append('displayNameMailAddress', displayNameMailAddress);

    $.ajax({
        type: 'POST',
        url: pathRoot() + '/SendMail',
        cache: false,
        dataType: 'json',
        data: formData,
        success: function (result) {
            if (result.shouldSeeError) {
                alertify.error(result.message);
            }
            else {
                alertify.success(result.message);
                //$('#emailForm').reset();
            }
        },
        error: function (result) {
            alertify.error("Error.");
        }
    });
});

Server side code (C#):

 [HttpPost]
 public ActionResult SendMail(string subject, string body, string displayNameMailAddress)
    {
        string message = string.Empty;
        bool error = false;
        try
        {
            Manager.Email.SendEmail(subject, body, displayNameMailAddress);
        }
        catch (Exception)
        {
            error = true;
        }

        message = error != true ? "success" : "error.";
        Utils.JsonHelper jsonH = new Utils.JsonHelper()
        {
            shouldSeeError = error,
            message = message
        };

        if (error == true)
            return Json(jsonH, JsonRequestBehavior.DenyGet);
        else
            return Json(jsonH, JsonRequestBehavior.AllowGet);
    }

Can anybody help me?

Thanks.

  • 3
    I'm guessing if you actually log the error, it's a "parseError", and it's because you're not returning valid JSON, but that's just a guess, seeing as you didn't post the serverside code. – adeneo Nov 23 '16 at 14:37
  • I have edited my question with the server side code. Can you help me better now? Thanks!! – João Estrela Nov 23 '16 at 15:06
  • My guess is your problem lies in `Utils.JsonHelper` whatever that is. You still need to confirm if your returning valid json or not. Check your json against a [json validator](http://jsonlint.com/). Basically this is still a dupe and should remain closed – Liam Nov 23 '16 at 15:10

0 Answers0