1

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.

6
  • 2
    Have you had a look in Chrome tools or Firebug to make sure that the URL is being hit and returning a non-error status code? Commented Jan 17, 2013 at 21:15
  • How do I do that please? Any help is greatly appreciated Commented Jan 17, 2013 at 21:21
  • Hit F12 in Chrome and the dev tools will display at the bottom of the browser window. Click the Network tab and you can view the HTTP traffic for your site. Commented Jan 17, 2013 at 21:25
  • You're missing { on the first line, after function(id) - dunno if that's a typo, or if it will have any effect though. If it is in your code, it could be causing your function to just be alert("here"), which would explain your behaviour - I'm not sure what the browser might do to try to cope. Commented Jan 17, 2013 at 21:26
  • 2
    Hit F12 key and click the "Network" tab and then give it a go to see if a request is being made. You should also set a failure callback function as well to print out any possible errors. You may not see anything without that if you're getting errors. Commented Jan 17, 2013 at 21:26

3 Answers 3

1

The specified url was wrong. Instead of url: '/HomeController/MethodName' I put url: '/Home/MethodName', although my controller name is HomeController

Sign up to request clarification or add additional context in comments.

Comments

1

You should rather let the power of the MVC framework take care of rendering the URL. If you change your routing it would possibly mean you have to go around all your html pages and updates similar references. In this way you avoid the above mentioned issue.

Use

<%= Html.RenderAction("MethodName", "Home") %>

Etc

function(id) {
    alert("here");
    $.ajax({
        url: <%= Html.RenderAction("MethodName", "Home") %>,
        data: { id: id },
        success: function(data){
             alert('Sucess ' + data);      
        }
    });
}

Comments

0

Your made a formatting mistake. This url string property is not correclty closed. You are also missing a comma at the end of the data property.

function(id)
 alert("here");
 $.ajax({
   url: '/HomeController/MethodName', // last single quote was missing 
   data: { id: id }, // comma was missing
   success: function(data){
    alert('Sucess ' + data);      
  }
 });

1 Comment

my bad. typo error in this post. My code has no typos but does not still work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.