简体   繁体   English

Spring Boot中的jQuery AJAX请求上的404

[英]404 on jQuery AJAX request in Spring boot

I have recently created a Spring boot project and am trying to work with jQuery AJAX to get data from Controller in Spring. 我最近创建了一个Spring引导项目,正在尝试与jQuery AJAX一起从Spring的Controller中获取数据。 It returns 404 for some reason. 由于某种原因,它返回404。

My controller 我的控制器

import java.util.ArrayList;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping(value="/", method=RequestMethod.GET)
    public String loadHome() {
        return "index";
    }

    @RequestMapping(value="/getMajors", method=RequestMethod.GET)
    public ArrayList<String> getMajors(){
        ArrayList<String> majors = new ArrayList<>();
        majors.add("Computers");
        majors.add("Physics");
        return majors;
    }

}

The first method was to load the home page index.html the second was the method that maps to AJAX request. 第一种方法是加载主页index.html ,第二种方法是映射到AJAX请求。

My jQuery AJAX Request 我的jQuery AJAX请求

$.ajax({
    type:"get",
    url:"/getMajors",
    success:function(data){
        console.log(data);
    },
    error:function(data){
        console.log(data);
    }
});

Please tell me why I'm getting a 404 here. 请告诉我为什么我在这里得到404。 TIA. TIA。

Edit: This app was created in Springboot. 编辑:此应用程序是在Springboot中创建的。 Standard config with web, dev-tools packages - from spring initializer. 带有Web和dev-tools软件包的标准配置-来自spring初始化程序。

Since you just want to return a data list,so you need to use @ResponseBody to let it only return the data list,change your code like below: 由于您只想返回数据列表,因此需要使用@ResponseBody使其仅返回数据列表,如下所示更改代码:

   @RequestMapping(value="/getMajors", method=RequestMethod.GET)
   @ResponseBody
   public ArrayList<String> getMajors(){
        ArrayList<String> majors = new ArrayList<>();
        majors.add("Computers");
        majors.add("Physics");
        return majors;
    }

Also,you need to be aware of ajax get method cache 另外,您需要知道ajax get方法缓存

I guess you are missing two things. 我想你错过了两件事。 One is the context name which usually is the name of the project or the war file deployed to the server, unless you are providing another context in web.xml. 一个是上下文名称,通常是部署到服务器的项目或war文件的名称,除非您在web.xml中提供了另一个上下文。 A sample servlet mapping from web.xml file: 来自web.xml文件的样本servlet映射:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

So when your ajax call hits /getMajors it usually points to " localhost:port/getMajors " instead of say " localhost:port/sampleproj/getMajors " .You are missing the context there. 因此,当您的ajax调用命中/getMajors它通常指向“ localhost:port/getMajors ”,而不是说“ localhost:port/sampleproj/getMajors ”。您在那里缺少上下文。

So your method needs to look like this: 因此,您的方法需要如下所示:

@RequestMapping(value = "/getMajors", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ArrayList<String> getMajors(){
        ArrayList<String> majors = new ArrayList<>();
        majors.add("Computers");
        majors.add("Physics");
        return majors;
}

One good method to debug these is to see the developer console(Chrome) for request response information. 调试这些错误的一种好方法是查看开发者控制台(Chrome)以获取请求响应信息。

Happy Coding! 编码愉快!

Edit 1 : removing @ResponseBody from method as the class is annotated with @RestController. 编辑1 :在类中使用@RestController注释时,从方法中删除@ResponseBody。 But my statement stands correct in case the class is annotated with @Controller. 但是,如果使用@Controller注释了该类,我的说法是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM