0

I need to access some c# code from javascript in MVC ,NET (not blazor). I have the below. This is very specific to .NET and MVC stack and not a generic ajax question like another one in stack overfow. I get no error message it just continues to the next statement after the ajax call. There could be 2 possible issues i think. My url: '/Home/CreatePostcodeFromCoordinates', is wrong. Or my C# assemply is not part of the assembly? or something similar. I am not that experienced with Web techs, I come from a DB background but can't be that difficult to get this link working right? Can't see anything else wrong. Also does the return value from C# need to be some special format or a string (as per now) is ok? this could be another reason? Thank you!

console.log("just before /Home/CreatePostcodeFromCoordinates"); $.ajax({ type: "POST", url: '/Home/CreatePostcodeFromCoordinates', data: { param1: longitude, param2: latitude }, success: function (response) { console.log('success'); console.log(response); }, error: function (error) { console.error(error); } }); 
5
  • 4
    it just continues to the next statement - yes, that's what the "a" in ajax means. Have your browser network tools (F12) open when your code runs and you should see a request made with a response code. eg 404 means wrong url. 200 means its all ok and should run your success: callback. Return value can be whatever string you want.
    – fdomn-m
    CommentedOct 28, 2024 at 14:40
  • So i get this Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Home/CreatePostcodeFromCoordinates I have the c# code under the HomeController.cs file which looks good i think cause the address is /Home/CreatePostcodeFromCoordinates but this is what i am not sure. Is the assembly created and visible to the app? What else can be wrong here in terms of an MVC .NET web app. Thanks!!
    – PanLondon
    CommentedOct 28, 2024 at 19:43
  • Your best bet will be to find a beginner tutorial on setting up an MVC app. eg: learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/…
    – fdomn-m
    CommentedOct 29, 2024 at 8:45
  • 404 means either the url is wrong or the back-end isn't running (or has something like [HttpGet] attribute, thus blocking POST, but that may give a different 4xx error).
    – fdomn-m
    CommentedOct 29, 2024 at 8:46
  • 1
    The nature of async programming could be an issue if you come from a realm where it's not present. Take a moment to read the linked question and redesign your javascript to embrace its async nature rather than working it around.CommentedOct 31, 2024 at 9:12

1 Answer 1

0

Finally this is how I managed to fix it:

I am a calling a C# method here in MVC. Make sure your C# method is not declared as static. Just like this. Also I spend probably 8 hours day trying to understand why I could not get any results back. You need the async:false option. Otherwise you will spend similar amount of time if not more!

C#

public string CreateCoordinatesFromPostcode(string postcode) { string result = "XXXXXX" // your logic return result; } 

JavaScript

 let result = ''; $.ajax({ type: "POST", url: '/clib/CreateCoordinatesFromPostcode', async: false, data: { postcode: postcode }, success: function (response) { result = response; }, error: function (error) { console.error(error); } }); console.log("just after /clib/createCoordinatesFromPostcode"); return result; 
3
  • 2
    async:false is deprecated (see the warning in the browser console). Instead, you should embrace the asynchronous nature. In this case, move the console.log "just after" to inside the success call and return $.ajax... then in the calling code use the return's promise's .then (one method).
    – fdomn-m
    CommentedOct 31, 2024 at 8:53
  • Consider also reading this articleCommentedOct 31, 2024 at 10:09
  • OK thanks will check it!
    – PanLondon
    CommentedOct 31, 2024 at 14:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.