AngularJS+SpringBoot旅游应用注册接口404问题求助
解决AngularJS注册请求404错误的方案
1. 修正请求目标地址
控制台报错显示请求发往了http://localhost:63343/registration,但你的SpringBoot后端默认运行在8080端口,这说明前端请求未指向正确的后端服务:
- 直接修改
register方法中的url为完整后端路径:url : 'http://localhost:8080/registration' - 或者配置AngularJS全局baseURL,避免每个请求重复写全路径:
travelApp.config(function($httpProvider) { $httpProvider.defaults.baseURL = 'http://localhost:8080/'; });
2. 确认后端接口存在
检查SpringBoot后端是否有对应POST /registration的处理接口:
- 确保存在类似如下的Controller代码:
@RestController public class UserController { @PostMapping("/registration") public ResponseEntity<?> registerUser(@RequestBody UserRegistrationDto registrationDto) { // 注册逻辑实现 return ResponseEntity.ok("注册成功"); } } - 如果后端配置了
server.servlet.context-path(例如/api),请求URL需要加上该上下文路径,即/api/registration。
3. 处理跨域问题
前端运行在63343端口,后端在8080端口,属于跨域请求,需在SpringBoot中配置跨域支持:
- 简单方式:在Controller类上添加
@CrossOrigin注解:@RestController @CrossOrigin(origins = "http://localhost:63343") public class UserController { // 接口方法实现 } - 全局配置方式:添加跨域配置类:
@Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:63343") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*"); } }
4. 验证后端接口可用性
直接通过Postman或浏览器发送POST http://localhost:8080/registration请求,携带JSON格式的注册数据,确认后端是否能正常响应。如果Postman也返回404,说明后端接口路径、请求方法存在问题,需重新检查代码。
5. 配置IntelliJ前端代理
如果通过IntelliJ直接运行前端页面,可配置代理自动转发请求到后端:
- 找到前端的Run Configuration,添加代理参数
--proxy http://localhost:8080,这样前端发起的请求会自动转发到后端服务。
内容的提问来源于stack exchange,提问作者Malik Zohaib Mustafa
相关产品推荐
相关产品推荐

