1

I'm new to .NET MVC. I'm trying to make an Ajax call to a .NET method, but it doesn't work. Please help.

Here is my Ajax code:

function resendConfirmationEmail()
{
    $("#resend-confirmation-email-status").html('<img src="../../Content/img/ajax-loader.gif" />');
    $.ajax({
        url: "/Ultility/ResendConfirmationEmail",
        type: "POST",
        datatype: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ "userID": $('#confirmation-email-userid').text().toString(), "subject": $('#confirmation-email-subject').text().toString() }),
        async: true,
        processData: true,
        cache: false,
        success: function (data) {
            $("#resend-confirmation-email-status").html("Email sent");
        }
    });
}

And here is my .Net method in UtilityController:

[HttpPost]
[WebMethod]
public JsonResult ResendConfirmationEmail(string userID, string subject)
{
    string destination = db.Users.Where(u => u.Id == userID).Select(u => u.Email).FirstOrDefault();
    Task<string> result = new AccountController().SendEmailConfirmationTokenAsync(userID, subject, destination);
    return Json(result, JsonRequestBehavior.DenyGet);
}
2
  • What do you mean by 'it doesn't work'?. Have you checked your browsers console log to see what's going on? Commented Jul 7, 2017 at 8:37
  • Yes, I have already checked the Ajax function in the browser, it worked. But it wouldn't call the .NET method. Commented Jul 8, 2017 at 3:11

5 Answers 5

3

Try removing the quotation marks in the data parameters of the ajax request

Like this:

function resendConfirmationEmail()
{
    $("#resend-confirmation-email-status").html('<img src="../../Content/img/ajax-loader.gif" />');
    $.ajax({
        url: "/Ultility/ResendConfirmationEmail",
        type: "POST",
        datatype: "json",
        contentType: "application/json; charset=utf-8",
        data: 
             JSON.stringify({ 
               userID: $('#confirmation-email-userid').text().toString(),
               subject: $('#confirmation-email-subject').text().toString() 
             }),
        async: true,
        processData: true,
        cache: false,
        success: function (data) {
            $("#resend-confirmation-email-status").html("Email sent");
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try this, but if you using asp.net mvc, i guess you don't need [WebMethod] attribute

    [HttpPost]
    [WebMethod]
public JsonResult ResendConfirmationEmail([FromBody] MyModel model)
{
  ....
}

public class MyModel{
   public string userID {get; set;}
   public string string subject {get; set;}
}

Comments

0

I think issue with your url where in use "url: "/Ultility/ResendConfirmationEmail"," insted of "url: "/Utility/ResendConfirmationEmail","

because your controller name is UtilityController

Comments

0
  1. Do not use Webmethod in .net method
  2. No need to use JSON.Stringify in ajax
    function resendConfirmationEmail()
        {
            $("#resend-confirmation-email-status").html('<img src="../../Content/img/ajax-loader.gif" />');
            $.ajax({
                url: "/Ultility/ResendConfirmationEmail",
                type: "POST",
                datatype: "json",
                contentType: "application/json; charset=utf-8",
               data: "{'userID': "+$('#confirmation-email-userid').text().toString()+",'subject': "+$('#confirmation-email-subject').text().toString()+" }",
                async: true,
                processData: true,
                cache: false,
                success: function (data) {
                    $("#resend-confirmation-email-status").html("Email sent");
                }
            });
        }

Comments

0
  1. Remove the [webmothod] data annotation from your action method,

  2. second option you are trying to pass json object for that u dont need to convert into string. Simply pass object

It wil work 👍

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.