简体   繁体   中英

Ajax call with Spring MVC Controller

I was trying to send an AJAX call to my controller, the code of which is displayed below. Now the problem that I am facing is that even though I am able to retrieve the data in the controller and subsequently process it, it is not returned to the jsp page with the AJAX call.

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

This is my AJAX call:

     $.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");
        }
    });

Attached below is a screen shot of the output: Eclipse Output

Instead of returning a Object. You should return a string with a ',' separator and split it to get the desired output in the view.

According to mozilla documentation , The ResponseText can be string or xml. You are passing a object which can be an issue.

Here's a link to get the comma separated String and use it in the view

So, the problem was with my url mapping. Before my question was edited, my code had some commented sections that were parsing the Person object and inserting its elements into to a JSON object. The problem was that the URL I was utilizing for my AJAX call had a .html extension and Spring actually uses the extension in the URL to decide what type of content to return, as mentioned in:

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

So by ammending the URL pattern in my web.xml like this:

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

And subsequently changing the url in my AJAX call to movie. 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");
        }
    });

I was able to achieve the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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