title: Unable to hit spring mvc controller through ajax call
body: <p>1.Below is my ajax call in index.jsp</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
              $('#buttonDemo1').click(function(){
                  $.ajax({
                     method: "GET",
                     url: '${pageContext.request.contextPath}/ajax/demo1',
                     success: function(result){
                         $('#result1').html(result);
                     },
                             error: function(response){
                                 console.log("error");
                             }
                  });
              });
          });
      &lt;/script&gt;
  </code></pre>
  
  <p>2.Below is controller's code</p>
  
  <pre><code>  @Controller
    @RequestMapping("/ajax")
    public class AjaxController {
      @RequestMapping(method = RequestMethod.GET)
      public String index() {
     return "ajax/index";
      }
  
      @RequestMapping(value="/demo1", method = RequestMethod.GET)
      @ResponseBody
      public String demo1() {
        return "Demo 1";
       }
     } 
  </code></pre>
  
  <p>3. Below is dispatcher's code</p>
  
  <pre><code>   &lt;?xml version='1.0' encoding='UTF-8' ?&gt;
     &lt;!-- was: &lt;?xml version="1.0" encoding="UTF-8"?&gt; --&gt;
     &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"&gt;
  
  &lt;context:component-scan base-package="demo.controller" /&gt;
  &lt;mvc:annotation-driven/&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/&gt;
  
  &lt;!--
  Most controllers will use the ControllerClassNameHandlerMapping above, but
  for the index controller we are using ParameterizableViewController, so we must
  define an explicit mapping for it.
  --&gt;
  &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
      &lt;property name="mappings"&gt;
          &lt;props&gt;
              &lt;prop key="index.htm"&gt;indexController&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp" /&gt;
  
  &lt;!--
  The index controller.
  --&gt;
  &lt;bean name="indexController"
        class="org.springframework.web.servlet.mvc.ParameterizableViewController"
        p:viewName="index" /&gt;
  </code></pre>
  
  <p></p>
  
  <ol start="4">
  <li><p>Below is web.xml</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
    &lt;web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"&gt;
    &lt;context-param&gt;
  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
  &lt;listener-
  class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-
  class&gt;
  &lt;/listener&gt;
  &lt;servlet&gt;
  &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
  &lt;servlet-
  class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
  &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;session-config&gt;
  &lt;session-timeout&gt;
      30
  &lt;/session-timeout&gt;
  &lt;/session-config&gt;
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;redirect.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre></li>
  </ol>
  
  <p>I don't know what's wrong, whenever I'm hitting controller's path in ajax I get "GET <a href="http://localhost:8080/ajax/demo1" rel="nofollow noreferrer">http://localhost:8080/ajax/demo1</a> 404 (Not Found)" error. Please help me out where I'm wrong. I've tried few solutions but they didn't resolve issue.</p>