3

I've been wrestling this error for quiet a while now and tried all the suggestions found on the web, still no luck.

I'm doing a simple ajax call to a controller's action. The application is in EPiServer, but this seems to be a generic question regarding using Ajax in ASP.NET.

Index View:

<input id="btnAjax" type="button" value="Favorite" />

<script>
    $('#btnAjax').click(function () {
        $.ajax({
            url: '@Url.Action("SetCookie", "Home")',
            contentType: 'application/html; charset=utf-8',
            type: 'POST',
            dataType: 'html'    
        })
        .success(function (result) {
            alert("Success");
        })
        .error(function (xhr, status) {
            alert(status);
        })
    });
</script>

Controller - HomeController:

    [HttpPost]
    public void SetCookie()
    { 
        //Do stuff        
    }

What I've done/tried:

  1. Referenced jquery correctly.

  2. Added the [WebMethod] attribute to the action

  3. Added a Http module in Web.config (ScriptModule, for EPiServer)

  4. Added Reference to jquery.unobtrusive-ajax.js, after the reference to jquery.

When I run the webapplication and push the button the debugger in Developer tool tells me

enter image description here

which is strange, since there is an action/method called SetCookie() in the HomeController. And the alert box "Fail" shows, which indicated that at least the js function is invoked.

Either I'm blind and missing something or there is something else which needs to be done...

All suggestions are welcome and much appreciated.

/ChrisRun

ChrisRun
  • 993
  • 1
  • 10
  • 24
  • if you change the action to `HttpGet`, can you access it using `http://yourdomain/Home/SetCookie`? – Dandy Oct 26 '15 at 10:35
  • @ChrisRun Your client calling code and web app hosted under the same server / domain? Install a program called Fiddler and see what request is going to your action and what reply you are getting. Also for testing purposes return `OK` from `SetCookie`. – lbrahim Oct 26 '15 at 13:25

2 Answers2

5

EPiServer does not register the default route for MVC by default. This is because you could have a page called "Home" in your site that would confict with the Home route. You can register the route yourself in Global.asax by overriding the RegisterRoutes method:

protected override void RegisterRoutes(RouteCollection routes)
    {
        base.RegisterRoutes(routes);

        // Register a route for Web API
        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "webapi/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // Register a route for classic MVC to use for API calls
        routes.MapRoute(
            name: "API",
            url: "api/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional });
    }

I like to put MVC and Web API routes under a prefix such as "webapi" or "api" so that it's less likely to conflict with any content routing.

Andreas
  • 1,355
  • 9
  • 15
0

Simply change your method from this:

public void SetCookie()
{ 
    //Do stuff        
}

to this:

public ActionResult SetCookie()
{ 
    //Do stuff        
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99