Calling a C# method from JavaScript

Nikolaj Nielsen

I want to to call a method GetAccount from my controller AccountController.cs, in my JavaScript factory LoginFactory.js. Something like this:

AccountController.cs:

public Account GetAccount(string userName)
    { ... }

LoginFactory.js:

if(x>y) {
   var account = <%AccountController.GetAccount(someParam);%>
}

I've tried using [WebMethod] and Ajax, but I can't get it to work: I get a 404 response.

Alex

Assuming your GetAccount method can be reached at /Account/GetAccount when your application runs, you could use the following:

$.ajax({
  type: 'GET',
  url: '/Account/GetAccount',
  data: { 'username' : 'a-username' },
  dataType: 'json',
  success: function(jsonData) {
    alert(jsonData);
  },
  error: function() {
    alert('error');
  }
});

Note - this is dependant on jQuery.

This causes the browser to make a request to /Account/GetAccount as if you had done so by entering the URL in the URL bar, but of course, captures the returned json for use in your client side (javascript) script.

If this returns a 404, it would be worth checking your routing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a java method from c++ in Android

From Java

Calling Java method from JavaScript using ScriptEngine

From Dev

Calling a template method from another template method in C++?

From Dev

Calling Rust method from C with array parameters

From Dev

calling C++ method from C code

From Dev

Calling C# from JavaScript in Xamarin

From Dev

Calling C++ method with callback from ObjectiveC

From Dev

Calling JavaScript object method from QML ListView delegate

From Dev

Calling method 2 classes down from current method C#

From Dev

calling a java servlet from javascript ajax post method

From Dev

Calling external Javascript from Action Method

From Dev

Calling javascript Service/Method from array object

From Dev

calling an object method with a setTimeout function from within the same object in Javascript

From Dev

MVC Calling ActionResult Method from JavaScript

From Dev

Method of calling functions in JavaScript

From Dev

Calling a javascript function from java method in Cordova

From Dev

Calling a method in a controller defined in a directive from a javascript function

From Dev

calling objective c method from callback

From Dev

Calling a method of a function constructor from another function in the global scope - Javascript

From Dev

Javascript - Calling a child method from inside a prototype method

From Dev

("NetworkError: 401 Unauthorized) Calling code behind method from JavaScript

From Dev

c++ calling method from subclass in superclass

From Dev

C# Calling base method from interface method

From Dev

Javascript OOP - calling a method from a different method of the same class

From Dev

Access calling object from Javascript method

From Dev

calling method from a new thread C

From Dev

Calling non-generic method from generic method in C#

From Dev

Calling Javascript prototype method from another method

From Dev

javascript why calling a method from another method in the same class need this?

Related Related

  1. 1

    Calling a java method from c++ in Android

  2. 2

    Calling Java method from JavaScript using ScriptEngine

  3. 3

    Calling a template method from another template method in C++?

  4. 4

    Calling Rust method from C with array parameters

  5. 5

    calling C++ method from C code

  6. 6

    Calling C# from JavaScript in Xamarin

  7. 7

    Calling C++ method with callback from ObjectiveC

  8. 8

    Calling JavaScript object method from QML ListView delegate

  9. 9

    Calling method 2 classes down from current method C#

  10. 10

    calling a java servlet from javascript ajax post method

  11. 11

    Calling external Javascript from Action Method

  12. 12

    Calling javascript Service/Method from array object

  13. 13

    calling an object method with a setTimeout function from within the same object in Javascript

  14. 14

    MVC Calling ActionResult Method from JavaScript

  15. 15

    Method of calling functions in JavaScript

  16. 16

    Calling a javascript function from java method in Cordova

  17. 17

    Calling a method in a controller defined in a directive from a javascript function

  18. 18

    calling objective c method from callback

  19. 19

    Calling a method of a function constructor from another function in the global scope - Javascript

  20. 20

    Javascript - Calling a child method from inside a prototype method

  21. 21

    ("NetworkError: 401 Unauthorized) Calling code behind method from JavaScript

  22. 22

    c++ calling method from subclass in superclass

  23. 23

    C# Calling base method from interface method

  24. 24

    Javascript OOP - calling a method from a different method of the same class

  25. 25

    Access calling object from Javascript method

  26. 26

    calling method from a new thread C

  27. 27

    Calling non-generic method from generic method in C#

  28. 28

    Calling Javascript prototype method from another method

  29. 29

    javascript why calling a method from another method in the same class need this?

HotTag

Archive