spring mvc中通过ajax请求台后,请求不到
配置如下:web.xml中DispatcherServlet的配置:<servlet><servlet-name>springMVC</servlet-name><ser...
配置如下:
web.xml中DispatcherServlet的配置:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 多个值用逗号分隔 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/META-INF/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
springMVC.xml的配置:
<!-- 启动扫描所有的controller -->
<context:annotation-config />
<context:component-scan base-package="com.zdy.web.*"/>
<mvc:annotation-driven/>
<!-- jsp页面解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
controller类:
@Controller
@RequestMapping("/hello/default.do")
public class HelloController {
@RequestMapping(params = "method=sayHello")
public ModelAndView sayHello(HttpServletRequest request) {
Hello hello = new Hello(RequestUtil.getMap(request));
System.out.println("sayHello");
return new ModelAndView("jsonView", hello.syaHello());
}
}
ajax请求
js方法:
function ajaxGet(url,data,success){
$.ajax({
url: url,
type: "GET",
data: data ,
dataType: "json",
success: success,
error: function(XMLHttpRequest,textStatus,errorThrown){
alert(errorThrown);
}
});
}
请求:
ajaxGet("/hello/default.do?method=sayHello",{name:"sjh"},function(){
alert("A");
});
ajax 错误提示为 "not found",为什么老请求不到呢,我写的url有什么问题吗?求解... 展开
web.xml中DispatcherServlet的配置:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 多个值用逗号分隔 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/META-INF/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
springMVC.xml的配置:
<!-- 启动扫描所有的controller -->
<context:annotation-config />
<context:component-scan base-package="com.zdy.web.*"/>
<mvc:annotation-driven/>
<!-- jsp页面解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
controller类:
@Controller
@RequestMapping("/hello/default.do")
public class HelloController {
@RequestMapping(params = "method=sayHello")
public ModelAndView sayHello(HttpServletRequest request) {
Hello hello = new Hello(RequestUtil.getMap(request));
System.out.println("sayHello");
return new ModelAndView("jsonView", hello.syaHello());
}
}
ajax请求
js方法:
function ajaxGet(url,data,success){
$.ajax({
url: url,
type: "GET",
data: data ,
dataType: "json",
success: success,
error: function(XMLHttpRequest,textStatus,errorThrown){
alert(errorThrown);
}
});
}
请求:
ajaxGet("/hello/default.do?method=sayHello",{name:"sjh"},function(){
alert("A");
});
ajax 错误提示为 "not found",为什么老请求不到呢,我写的url有什么问题吗?求解... 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询