我在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>
我有控制器如下。
@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
}
}
现在如何在jquery ajax调用中指定URL?
$.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);
}
});
答案 0 :(得分:0)
通过查看上面的代码,你应该能够转到下面的url(假设8080端口是默认的Tomcat端口)。
http://localhost:8080/getData.do?geo=1&tab=1&gid=1
这应该会在浏览器中显示您需要的JSON。如果JSON出现在页面上,只需从jQuery中$.getJSON()
,因为它内置了用于撤回JSON的方法。您可以查看有关此方法的文档here.