-2

I am doing a .NET project and faced with a problem that I need to connect a c# method to a javascript script

//more detail

I need to put javascript variables with data in a c# method and call method in javascript

Thanks in advance

Alex Hard
  • 35
  • 4
  • 1
    Do you understand [the difference between client-side code and server-side code](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming)? You can send an AJAX call. – jabaa Mar 02 '23 at 13:59

1 Answers1

1

Asynchronous JavaScript And XML (AJAX)

To connect JavaScript and C# code you need to use AJAX this is where in JavaScript you write a piece of code which will send a Request to the C# Server. For the AJAX Request you will tell it multiple things such as

  1. Location - This is where the serve will Get/Post data from/to
  2. Type - This is where you declare if you want to Get or Post data
  3. Data - This is the data that you declare what is required or going to be sent
  4. Success - This is where code is run when the data is posted or Received Successfully
  5. Error - This is where code is run when the data is not posted or not Received Successfully

Example in JQuery

$.ajax({
        url: url,
        type: 'post',
        data: data,
        success: function (data) {
            //Some code here
        },
        error: function (request, status, error) {
            //If theres data request do this instead
        }
    });

A link to the AJAX Documentation https://api.jquery.com/jquery.ajax/

Hope this helps