How to call a controller method from jQuery?

I have ajax code for asp.net (non-mvc) to call webMethod to get additional data from the server for the request. But I can’t understand what the URL is for my jQuery in MVC.

<script type="text/javascript" language="javascript"> function SubmitAjax(url, message, successFunc, errorFunc) { $.ajax({ type:"POST", url:url, data:message, contentType: "application/json; charset=utf-8", dataType: "json", success:successFunc, error:errorFunc }); }; 

I do not want to transfer the entire list of related data to the selection list for each person who comes to the page, because not everyone needs it. Therefore, I would like to call a controller or web method via jQuery, but I cannot figure out how to address the URL in MVC.

I noticed this post: jQuery Ajax call is allowed for the current controller folder, instead of the root folder

is the $ .getJson and mvc method? Is this a good solution for my use case? I need only return a string url or an empty string if what I am looking for is not found. Do I need to include the $ .getJSon method? is part of MVC or part of jQuery? Is the leading slash an indication of the root of the application or the root of the server?

+7
jquery c # asp.net-mvc
source share
5 answers

The problem was the webMethod tag on the controller, which was necessary or useful in asp.net, but not in asp.net mvc

0
source share

Try the following post: Basic AJAX example with ASP.NET MVC?

or this one: How to delete a Json object to delete?

They should give you some pointers.

In essence, $ .getJson is a jQuery method, not an MVC, but use it to use it in conjunction with your MVC controller that returns a Json result.

+3
source share

Well, I assume you are having trouble building the URL. Unless you have a slash in front of the URL, this will apply to your current URL. So, if the current url is: /Home/Index , and you have a link like this: <a href="Ticket/CheckForInstaller">Text</a> , then this link will point to the following URL: /Home/Index/Ticket/CheckForInstaller . This is always browser behavior. The same thing happens if you have a page in a folder in your webforms application. There is nothing else with the asp.net mvc url and then with any other web card. You might need the url: /Ticket/CheckForInstaller .

However, the asp.net mvc framework provides helpers for you, so you don't need to hardcode any URL. You can do it like this:

 <%=Url.Action("Ticket", "CheckForInstaller")%> 

But the only thing that will do this is to find the appropriate URL that points to this action ("/ Ticket / CheckForInstaller", depending on your routes) and write it down.

+2
source share

You can use $ .getJSON (url, [data], [callback (data)]), where data is your returned json data object.

Alternatively, you can use $ .post (url, [data], [callback (data)]), where you re-update the row / data object.

url: this is the relative url of your controller / action / script, which returns json / data back.

The above jquery methods and you can use if you included jquery js file.

+1
source share

This is what I have been doing lately and it works great.

  • Get the root path and save it in hidden input (replace [with <)

    [input id = "urlBase" type = "hidden" value = "<% = Url.Content (" ~ ")%>" / ">

  • Get the value of this input and save it in js (using jquery in this case)

    _urlBase = $ ("# urlBase"). val ();

  • Use this variable to call controllers.

    $. ajax ({type: "GET", url: _urlBase + "Controller / Action", dataType: "json"});

+1
source share

All Articles