Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX在springmvc中返回404_Ajax_Spring_Spring Mvc - Fatal编程技术网

AJAX在springmvc中返回404

AJAX在springmvc中返回404,ajax,spring,spring-mvc,Ajax,Spring,Spring Mvc,ViewResolver(我的jsp位于前缀值中指定的正确文件夹中): 在STS IDE上打开Tomcat 8服务器后,使用此urlhttp://localhost:8080/home.fst工作正常 但是在页面上,像下面这样调用AJAX会抛出一个404错误: $.ajax({ type: "POST", url: "/testAjax.fst", data: {"memberId" : "test"}, success: function (result) {

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

在STS IDE上打开Tomcat 8服务器后,使用此url
http://localhost:8080/home.fst
工作正常

但是在页面上,像下面这样调用AJAX会抛出一个
404
错误:

$.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
控制器,而且服务器上并没有错误日志

logger.info("Text Ajax action has been executed. My Parameter is " + id);
当我的AJAX调用
textAjax
操作时,日志也会打印出来。我也用debug point检查了它(它坏了)


有什么问题吗?

一切都很好,只需在方法中添加
@ResponseBody
注释,而且我建议您将请求方法
POST
更改为
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)
    } 
});

在中仅使用/testAjax进行尝试url@HarshalPatil比尝试使用
.fst
更糟糕。它甚至无法调用控制器。是。我认为您需要给出根url,然后再试一次,比如在url中指定项目名称,然后/testAjax它就会工作。@HarshalPatil两者都不工作。。谢谢。你能解释一下为什么添加
@ResponseBody
可以解决这个问题吗?不客气:)。因为我们希望内容不是整个jsp文件。简单地说,@ResponseBody将直接返回内容,而无需任何JSP。我认为POST也是可能的,而且总是发送数据的更好方法。主要的解决方案是@ResponseBody。
logger.info("Text Ajax action has been executed. My Parameter is " + id);
@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;
}
$.ajax({
    type: "GET", //Made Change
    url:"/testAjax.fst",
    data: {"memberId" : "test"},
    success: function (result) {
    console.log(result)
    } 
});