AJAX在Spring MVC中返回404 [英] AJAX returns 404 in Spring MVC

查看:104
本文介绍了AJAX在Spring MVC中返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ViewResolver(我的jsp位于前缀值指定的正确文件夹中):

ViewResolver (my jsp is in the right folder as specified on prefix value):

<!-- Resolves views selected for rendering by @Controllers --> 
<!-- to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

Servlet映射:

Servlet mapping:

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.fst</url-pattern>
</servlet-mapping>

控制器:

@Controller
public class HomeController {   
    private static final Logger logger = 
        LoggerFactory.getLogger(HomeController.class);

    @RequestMapping("/home")
    public ModelAndView home(String user, HttpServletRequest request) {
        logger.info("Home controller has been executed");
        ModelAndView mv = new ModelAndView();

        mv.addObject("userName", user);
        mv.addObject("controllerName", request.getRequestURI());
        mv.setViewName("home");

        return mv;
    }

    @RequestMapping(value = "/testAjax", method = RequestMethod.POST)
    public String testAjax(@RequestParam("memberId") String id, 
        HttpServletRequest request, HttpServletResponse response, 
        Locale locale, Model model) {

        logger.info("Text Ajax action has been executed. My Parameter is " + id);

        return id;
    }
}

在STS IDE上打开Tomcat 8服务器后,使用此URL http://localhost:8080/home.fst 可以访问此网站.

After turning on Tomcat 8 server on STS IDE, accessing this web with this url http://localhost:8080/home.fst works okay.

但是在页面上,如下所示调用AJAX会引发 404 错误:

But on the page, calling AJAX like below throws a 404 error:

$.ajax({
    type: "POST",
    url: "/testAjax.fst",
    data: {"memberId" : "test"},
    success: function (result) {
        console.log(result)
    } 
});

这是控制台错误日志:

 POST http://localhost:8080/testAjax.fst 404 (Not Found)
 k.cors.a.crossDomain.send                     jquery-2.1.3.min.js:4
 n.extend.ajaxhome.fst:11 (anonymous function) jquery-2.1.3.min.js:3
 n.event.dispatch                              jquery-2.1.3.min.js:3
 r.handle

奇怪的是,它调用 testAjax 控制器就很好,并且服务器上没有错误日志.

Strange thing is that it calls testAjax controller just fine and there's no error log on server.

logger.info("Text Ajax action has been executed. My Parameter is " + id);

当我的AJAX调用 textAjax 操作时,也会打印日志.我也用调试点检查了一下(它坏了).

When textAjax action is invoked by my AJAX, the log is printed as well. I checked it out with debug point too (it broke alright).

怎么回事?

推荐答案

一切都很好,只需在您的方法中添加 @ResponseBody 批注,并且我建议您更改请求方法 POST GET

Everything's good just Add @ResponseBody annotation in your method and also I suggest you to change your request method POST to GET

春天

@RequestMapping(value = "/testAjax", method = RequestMethod.GET) //Made Change
@ResponseBody //added
public String testAjax(@RequestParam("memberId") String id,     HttpServletRequest request, HttpServletResponse response, Locale locale, Model model) {
    logger.info("Text Ajax action has been executed. My Parameter is " + id);

    return id;
}

JQuery

$.ajax({
    type: "GET", //Made Change
    url:"/testAjax.fst",
    data: {"memberId" : "test"},
    success: function (result) {
    console.log(result)
    } 
});

这篇关于AJAX在Spring MVC中返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆