jqueryasp.net-mvc

Entering the controller again from the page via Ajax


I have started ASP.NET MVC entering my controller and sending out the view. I am also writing a log.

The page has jQuery and JavaScript so I need to be able to enter into the controller again, preferably not a postback but via Ajax or similar. Is this possible, or do I call an action on a controller from JavaScript?


Solution

  • Well if you want to do ajax against a controller you can do an .ajax with jQuery like this:

    $.ajax({
                     type: "GET",//type of verb to use POST/GET
                     data: ({ Data:data }),//Send some data to the controller
                     url: '<% = Url.Action("MEthod","Controller")%>',//URL of the resource
                     dataType: "text/html", //Define the type of spectated results(JSON,Html,Xml)
                     timeout: 8000, //How much time to wait for the asynchronous call to complete
                     error: function() {
                         alert('ERROR');
                     }, //end error
                     success: function(html) {
                     //Here do something with the result hold in the html variable  
                     } //end succes
                 });   //end ajax
    

    For a complete reference you can visit the jQuery Documentation http://docs.jquery.com/Ajax