0

I am trying to call a C# function on a button click using JavaScript/jQuery function.

I went through similar issues and their solutions, but none fits this case. My C# function does not return anything. It's a function that triggers a job from the task scheduler. Is there a different way to write the JavaScript function, where the function does not return anything?

Below is the code.

HTML:

<button id="btnClick" runat="server" EnablePageMethods="true">Replay</button>

JavaScript:

$(document).ready(function() {
  $(".btnClick").on('click', function() {
    $.ajax({
      async: true,
      url: "Home/ABVReplay",
      cache: false,
    });
    console.log("Replayed!");
  });
});

C#:

[WebMethod]
public void ABVReplay()
{
    using (TaskService tasksrvc = new TaskService("<server.name>", "<username>", "<domain>", "<password>"))
    {
        Task task = tasksrvc.FindTask("<taskName>");
        task.Run();
    }

}

The job runs if I just use the C# code in a console application. So, there is no connection/login issue as such.

Console app code:

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
    static void Main(string[] args)
    {
        using (TaskService tasksrvc = new TaskService("<server.name>", "<username>", "<domain>", "<password>"))
        {
            Task task = tasksrvc.FindTask("<taskName>");
            task.Run();
        }
    }
}
9
  • 1
    What's your webservice url? Commented Jul 19, 2018 at 21:35
  • Sorry, just edited the url. Home is the Controller and ABVReplay is the function. Commented Jul 19, 2018 at 21:43
  • @D-Shih I tried your suggestion, still didn't work and yes, the url is correct. Commented Jul 19, 2018 at 22:14
  • Please use the Chrome Developer Tools to show us the Request Headers and Response Headers for the AJAX request. Commented Jul 19, 2018 at 22:23
  • Could the program executed in the webserver by the URL when you call by browse URL? Commented Jul 19, 2018 at 22:23

2 Answers 2

4

Your jquery selector need to change

$("#btnClick")

instead of

$(".btnClick")

and make sure your call URL is correct.

# select html tag id

. select html tag class

Sign up to request clarification or add additional context in comments.

Comments

0

. represents jquery class and # represents id, you have to replace for this code, and should works.

$(document).ready(function () {
  $("#btnClick").on('click', function () {

    $.ajax({
        async: true,
        url: "Home/ABVReplay",
        cache: false,
     });
    console.log("Replayed!");
  });
});

1 Comment

also, try with : url: "/Home/ABVReplay", What message do you get in your browser console? after click the button?

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.