Click here to Skip to main content
12,250,675 members (40,510 online)
Rate this:
 
Please Sign up or sign in to vote.
This seems easy but is not just working for me. I have the following code to run a method in my HomeController from my javascript file
function(id)
 alert("here");
 $.ajax({
   url: '/HomeController/MethodName,
   data: { id: id }
   success: function(data){
    alert('Sucess ' + data);      
  }
 });
My method is
public string MethodName(int id)
{
   return id.ToString() + "test ";
}


The view calling the javascript has the following scripts defined: jquery.unobtrusive-ajax.min.js, MicrosoftMvcAjax.js, MicrosoftAjax.js, validate.unobtrusive.min.js, jquery.validate.min.js

But is is not just working. Nothing happens. The first alert of the function do show up. But nothing else after that.
Posted 17-Jan-13 8:00am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Close, but not quite how it works. Here is a sample of how it should look.
$.ajax({
     type: 'GET',
     url: '/YourController/YourMethod',
     dataType: 'json',
     async: false,
     data: { arg1: 'arg1value',
             arg2: 'arg2value'
           },
     success: function (result) { alert(result); },
     error: function (jqXHR, textStatus, errorThrown) {
               alert( 'An error occured while loading: ' + textStatus + ": " + errorThrown + ": " + jqXHR.responseText );
           }
       });
[HttpGet]
public JsonResult YourMethod(string arg1, string arg2)
{
   // Do something to get a result...
   string yourResult = "Some soft of result.";
   return Json(yourResult, JsonRequestBehavior.AllowGet);
}
  Permalink  
Comments
Espen Harlinn 17-Jan-13 18:12pm
   
5'ed!
Marcus Kramer 17-Jan-13 18:38pm
   
Thanks, Espen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100