【问题标题】:Ajax call with Spring MVC Controller使用 Spring MVC 控制器进行 Ajax 调用
【发布时间】:2016-08-31 22:50:53
【问题描述】:

我试图向我的控制器发送 AJAX 调用,其代码如下所示。现在我面临的问题是,即使我能够在控制器中检索数据并随后对其进行处理,它也不会通过 AJAX 调用返回到 jsp 页面。

@SuppressWarnings("unchecked")
@RequestMapping(value="/movie", method=RequestMethod.GET)
public @ResponseBody Person search(HttpServletRequest request, HttpServletResponse response) throws IOException{        
    String name = request.getParameter("uname1");
    System.out.println(name);
    List<Person> movie = personDAO.search(name);
    Person per = new Person();
    for (java.util.Iterator<Person> iterator = movie.iterator(); iterator.hasNext();){
        per = iterator.next(); 
    }

    System.out.print(per + " Wtf");
    return per;
}

这是我的 AJAX 调用:

     $.ajax({
        url: 'movie.html',
        dataType: "json",
        type: "GET",
        contentType: 'application/json',
        mimeType: 'application/json',
        data: 'uname1=' + $('#element0').val(),
        success: function(data){
               $('#col1').text(data.name);
               $('#col2').text(data.pname);
               $('#col3').text(data.wname);
               $('#col4').text(data.lname);
        },
        error: function(xhr, status, error) {
               $('#col1').text("Undefined");
               $('#col2').text("Undefined");
               $('#col3').text("Undefined");
               $('#col4').text("Undefined");
        }
    });

下面附上输出的屏幕截图: Eclipse Output

【问题讨论】:

    标签: jquery ajax spring spring-mvc


    【解决方案1】:

    而不是返回一个对象。您应该返回一个带有 ',' 分隔符的字符串并将其拆分以在视图中获得所需的输出。

    根据 mozilla documentation,ResponseText 可以是字符串或 xml。您传递的对象可能是个问题。

    这是一个link 来获取逗号分隔的字符串并在视图中使用它

    【讨论】:

      【解决方案2】:

      所以,问题出在我的网址映射上。在我的问题被编辑之前,我的代码有一些注释部分正在解析 Person 对象并将其元素插入到 JSON 对象中。问题是我用于 AJAX 调用的 URL 有一个 .html 扩展名,而 Spring 实际上使用 URL 中的扩展名来决定返回什么类型的内容,如下所述:

      @ResponseBody not working with spring 4.2.4 - not a duplicate I have checked all others

      因此,通过像这样修改我的 web.xml 中的 URL 模式:

      <servlet-mapping>
          <servlet-name>dispatcher</servlet-name>
          <url-pattern>*.html</url-pattern>
          <url-pattern>*.json</url-pattern>
      </servlet-mapping>
      

      随后将我的 AJAX 调用中的 url 更改为电影。JSON

           $.ajax({
              url: 'movie.json',
              dataType: "json",
              type: "GET",
              contentType: 'application/json',
              mimeType: 'application/json',
              data: 'uname1=' + $('#element0').val(),
              success: function(data){
                     $('#col1').text(data.name);
                     $('#col2').text(data.pname);
                     $('#col3').text(data.wname);
                     $('#col4').text(data.lname);
              },
              error: function() {
                     $('#col1').text("Undefined");
                     $('#col2').text("Undefined");
                     $('#col3').text("Undefined");
                     $('#col4').text("Undefined");
              }
          });
      

      我能够达到预期的结果。

      【讨论】:

        猜你喜欢
        • 2015-01-16
        • 1970-01-01
        • 2018-02-24
        • 2015-10-08
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多