简体   繁体   English

无法通过Ajax调用命中Spring MVC控制器

[英]Unable to hit spring mvc controller through ajax call

1.Below is my ajax call in index.jsp 1,下面是我在index.jsp中的ajax调用

    <script type="text/javascript">
        $(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");
                           }
                });
            });
        });
    </script>

2.Below is controller's code 2.下面是控制器的代码

  @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";
     }
   } 

3. Below is dispatcher's code 3.下面是调度员的代码

   <?xml version='1.0' encoding='UTF-8' ?>
   <!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
   <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">

<context:component-scan base-package="demo.controller" />
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
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.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

  1. Below is web.xml 下面是web.xml

      <?xml version="1.0" encoding="UTF-8"?> <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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener- class>org.springframework.web.context.ContextLoaderListener</listener- class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app> 

I don't know what's wrong, whenever I'm hitting controller's path in ajax I get "GET http://localhost:8080/ajax/demo1 404 (Not Found)" error. 我不知道这是怎么回事,每当我在ajax中命中控制器的路径时,都会出现“ GET http:// localhost:8080 / ajax / demo1 404(未找到)”错误。 Please help me out where I'm wrong. 请帮我解决我错了的地方。 I've tried few solutions but they didn't resolve issue. 我尝试了很少的解决方案,但是他们没有解决问题。

Try to add the application name to the url, 尝试将应用程序名称添加到url,

http://localhost:8080/[AppName]/ajax/demo1 HTTP://本地主机:8080 / [AppName的] / AJAX / demo1的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM