Click here to Skip to main content
Rate this: bad
good
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 9: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 at 17-Jan-13 18:12pm
   
5'ed!
Marcus Kramer at 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
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
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