Sitecore 7 MVC 4 & Ajax call

admin

Administrator
Staff member
I'm using Sitecore 7.1 and an MVC 4 project.

I'm trying to make a search box which will search news into a bucket and return the result as JSON.

So i read that blog post : <a href="http://keeplearningandsharing.wordpress.com/2013/12/13/sitecore-mvc-with-ajax/" rel="nofollow">http://keeplearningandsharing.wordpress.com/2013/12/13/sitecore-mvc-with-ajax/</a>

and try to do the same but it does not work.

What i did :

1) I created a controller named NewsOverviewController where i put two methods

Code:
ActionResult Start() (HttpGet) which return my view with the searchbox
JsonResult Starts(InpurtParams) which perform the search

2) In sitecore, i created my Controller Rendering and add the action start to it.

3) i register a new route in RouteConfig.cs (i put it before the sitecore route and after, didn't change anythings)

<pre class="lang-cs prettyprint-override">
Code:
  routes.MapRoute(
          name: "search",
          url: "api/NewsOverview/{action}/{id}",
          defaults: new { controller = "NewsOverview", action = "Starts", id = UrlParameter.Optional }
        );

4) I put the following code in my view :

Code:
&lt;script&gt;
function getSearchResult() {
    var inputparams = {};
    inputparams.SearchTerm = document.getElementById("searchTerm").value;

    try {
        $.getJSON("/api/NewsOverview/starts", inputparams, displaySearchResult);
    }
    catch (ex) {
        alert("Error : " + ex.message);
    }
}

function displaySearchResult(data) {
    $.each(data, function (i, result) {
        $("#searchresult").append(result.Name + " - " + result.Url + "&lt;br/&gt;");
    });

};
&lt;/script&gt;

&lt;div&gt;
    &lt;input type="text" name="searchTerm" id="searchTerm" value="test"/&gt;
    &lt;input type="button" onclick="getSearchResult();"/&gt;
    &lt;div id="searchresult"&gt;&lt;/div&gt;
&lt;/div&gt;

<strong>My problem when i try to use the search :
He didn't find the url (404 (Page not found) ) :
{host}/api/NewsOverview/start?SearchTerm=test</strong>

I'm pretty new to MVC, so i miss maybe somethings ...