Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a controller method from JQuery?

I have ajax code for asp.net (non-mvc) to call to a webMethod to get additional data from the server for a request. But I can't seem to figure out the url to give 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 don't want to pass the entire list of related data to a select list to every person that arrives at the page, as not all will need this functionality. So I'd like to call a controller or webmethod via JQuery, but can't seem to find out how to address the URL in MVC.

I noticed this post: JQuery Ajax call gets resolved to the current Controller Folder, Instead of root Folder

is $.getJson an mvc method? is this a good solution for my use case? I only need to return a string url, or an empty string if what i'm looking for is not found. Do I need to include a for the $.getJSon method? is that part of MVC or part of JQuery? Is the leading slash going to point to Application root or server root?

like image 503
Maslow Avatar asked Feb 10 '10 16:02

Maslow


3 Answers

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

or this one: How to get the Json object for drop down?

They should give you some pointers.

In essence, $.getJson is a jQuery method, not an MVC one, but use want to use that in combination with your MVC controller returning a Json result.

like image 125
Johannes Setiabudi Avatar answered Oct 14 '22 02:10

Johannes Setiabudi


Well, my guess is that you are having trouble with how url's are constructed. If you don't have a slash before the url it will be relative 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 that link will point to the following url: /Home/Index/Ticket/CheckForInstaller. This is always the behavior in the browser. The same thing happens if you have a page in a folder in a webforms application. There is nothing different with a asp.net mvc url then with any other web framework. The url you want is probably this one: /Ticket/CheckForInstaller.

The asp.net mvc framework does, however, supply you with helpers so that you don't have to hard code any url. You can do that like this:

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

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

like image 38
Mattias Jakobsson Avatar answered Oct 14 '22 04:10

Mattias Jakobsson


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 data is you returened string / data object.

url : it is the relative url to your controller/action/script which returns json/data back.

The above are jquery methods and you shoul be able to use if you have included jquery js file.

like image 29
Nrj Avatar answered Oct 14 '22 03:10

Nrj