【问题标题】:Not getting response from controller in Spring MVC when using ajax使用ajax时没有从Spring MVC中的控制器获得响应
【发布时间】:2014-08-27 16:29:05
【问题描述】:

我在 Spring MVC 3 中使用 eclipse kepler ide 的 Ajax 调用控制器有问题。我在 youtube 上找到了一些教程并制作了学习项目。当我对控制器进行 Ajax 调用时,我也遇到了较早的问题,但一段时间后可以正常工作。我的项目是在 Spring MVC 项目中制作的,我有所有请求 jar,只是没有通过 Ajax 调用访问控制器。 thise 是我在 jquery 中的 conf 文件、控制器方法和 ajax 调用

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml

<mvc:resources location="/resources/" mapping="/resources/**"/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean>

<context:component-scan base-package="com.milan.springajax.controller" />

我的控制器方法

@RequestMapping(value="/getJSON/{firstName}/{lastName}", method=RequestMethod.GET)
public  @ResponseBody
Contact findByName(@PathVariable("firstName") String first, @PathVariable("lastName") String last,
        HttpServletRequest request, HttpServletResponse response){
    Contact contact=contactService.findByName(first, last);
    return contact;
}

和ajax调用谁位于jsp页面中&lt;script&gt;标签

$(document).ready(function () {
$('.button').on('click', function () {
    var first=$('#firstInput').val();
    var last=$('#lastInput').val();
    alert("button je stisnut " + first + " " + last); 

    $.ajax({
        type:"GET",
        url:'${pageContext.request.contextPath}/getJSON/' + first + "/" + last,
        dataType:'json',
        success: function(result) {
            var contact = "id: " + result.id +
                " | name : " + result.firstName + " " result.lastName + 
                " | age : " + result.age;


            $('#theJson').html(contact);

        },

         error: function(jqXHR, textStatus, errorThrown) {
            alert("Contact " + textStatus  + errorThrown + " !");
        }  in thise line i get warning that i miss semicolon ???
});

 });
}); 

和html元素在同一个jsp页面中

        <div>
            <label for="firstInput">First Name</label>
            <input type="text" name="firstName" id="firstInput">
        </div>
        <div>
            <label for="laststInput">Last Name</label>
            <input type="text" name="lastName" id="lastInput">
        </div>
        <div id="theJson"></div>
        <button type="button" class="button" id="button">Fetch JSON</button>

是否有其他人在使用 spring mvc 时无法在 eclipse 中响应的 ajax 调用有问题,就像调用 thise ajax 方法时没有点击正确的 url 地址一样。

【问题讨论】:

  • 尝试使用浏览器并使用 firebug 来检查请求
  • 您的网络控制台显示什么?

标签: java jquery ajax eclipse spring-mvc


【解决方案1】:

从网址${pageContext.request.contextPath}/getJSON/' + first + "/" + last中删除${pageContext.request.contextPath}

它不适用于 JS 文件,而是直接替换为您的应用上下文:
your-app/getJSON/' + first + "/" + last/getJSON/' + first + "/" + last

【讨论】:

  • var contact = "id: " + result.id + " | name : " + result.firstName + " " result.lastName + " | age : " + result.age; -- 这行代码有问题,我忘了 + 到 result.lastName 呵呵。谢谢大家的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2013-05-08
  • 2018-12-05
  • 1970-01-01
相关资源
最近更新 更多