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);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Luan Tran
  • 376
  • 1
  • 3
  • 15
  • What do you mean by 'it doesn't work'?. Have you checked your browsers console log to see what's going on? – PeteG Jul 07 '17 at 08:37
  • Yes, I have already checked the Ajax function in the browser, it worked. But it wouldn't call the .NET method. – Luan Tran Jul 08 '17 at 03:11

5 Answers5

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");
        }
    });
}
Rute Lemos
  • 191
  • 1
  • 11
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;}
}
Andrey Ravkov
  • 1,268
  • 12
  • 21
0

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

because your controller name is UtilityController

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");
                }
            });
        }
OldTrain
  • 1,880
  • 1
  • 25
  • 22
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

Rahul Shukla
  • 646
  • 6
  • 19