简体   繁体   中英

Jquery ajax call to invoke spring controller?

I have below config in web.xml

<servlet>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
         </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>

    <servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>*.do</url-pattern>  
    </servlet-mapping>

I have controller as below.

 @Controller  
    public class SomeController { 

       @RequestMapping(value = "/getData", method = RequestMethod.GET)
    public ModelAndView showExtendedUi(@RequestParam("geo") String geo, @RequestParam("tab") String tab, @RequestParam("gid") String gid, HttpServletResponse response) {  
        //logic
    } 

    }  

Now how can i specify URL in jquery ajax call?

 $.ajax({
            type: "GET",
            url: "getData.do",
            dataType: "json",
            success: function(responseJson) {
                alert("json"+responseJson);
            },
            error: function(xhr, status, error) {
                alert('Failed to get details: ' + error);
            }
        });

From looking at the code above, you should just be able to go to the following url (assuming 8080 port as default Tomcat port).

http://localhost:8080/getData.do?geo=1&tab=1&gid=1

This should show you in a browser the JSON you require. If the JSON appears on the page here, just do $.getJSON() from jQuery as it has built in methods for pulling back JSON. You can see the documentation on this method here.

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