sign(examCode,userId){ axios.post( "/api/sign",{examCode:examCode,studentId:userId,motion:'申请成功'}, {headers: {'Content-Type': 'application/json; charset=utf-8'}} ).then(function (res) { console.log(res); }).catch(function (error) { alert("已申请"); }) },
时间: 2023-11-22 13:52:40 浏览: 27
这段代码是一个前端 JavaScript 的函数,用于向后端发送一个 POST 请求,请求参数包括考试代码 examCode、学生 ID userId、以及一个 motion 字段表示申请成功。请求的接口地址为 "/api/sign"。请求成功后,会在控制台打印出响应结果 res;如果请求失败,则会弹出一个提示框提示用户已经申请过了。其中,请求头中设置了 Content-Type 为 application/json,表示请求体中的数据格式为 JSON。
相关问题
@GetMapping("/exam/{examCode}") public ApiResult findById(@PathVariable("examCode") Integer examCode){ // System.out.println("根据ID查找"); ExamManageEntity res = examManageService.findById(examCode); if(res == null) { return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null); } return ApiResultHandler.buildApiResult(200,"请求成功!",res); }
这段代码是一个基于Spring框架的RESTful API接口的实现,使用了@GetMapping注解来指定HTTP GET请求方法的路径。其中,"{examCode}"表示动态的URL地址,该地址中的参数会被传递给方法中的@PathVariable注解修饰的参数examCode。方法的返回值类型是ApiResult类,通过调用examManageService中的findById方法查找相应的考试信息,并将结果封装到ApiResult中返回给客户端。如果根据考试编号查找不到考试信息,则返回错误码10000和错误信息“考试编号不存在”,否则返回状态码200和考试信息。
@GetMapping("/exam/{examCode}/{userId}") public ApiResult findNotHadJoinedExamByUserId(@PathVariable("examCode") Integer examCode, @PathVariable("userId")Integer userId) throws BusinessCodeException { Optional.ofNullable(userId).orElseThrow(() ->{ return new BusinessCodeException(BusinessCode.SELECT_ERROR_CODE.getMsg(),BusinessCode.SELECT_ERROR_CODE.getCode()); }); Optional.ofNullable(examCode).orElseThrow(() ->{ return new BusinessCodeException(BusinessCode.SELECT_ERROR_CODE.getMsg(),BusinessCode.SELECT_ERROR_CODE.getCode()); }); ExamManageEntity res = examManageService.findNotHadJoinedExamByUserId(examCode,userId); if(res == null) { return ApiResultHandler.errorResult(BusinessCode.USER_HIVED_JOIN_THE_EXAM.getCode(), BusinessCode.USER_HIVED_JOIN_THE_EXAM.getMsg()); } return ApiResultHandler.successResult(res); }
这是一个 Spring Boot 的控制器(Controller)中的一个方法,使用了 GetMapping 注解,映射了一个 GET 请求的 URI 路径 "/exam/{examCode}/{userId}",其中 examCode 和 userId 是路径参数。该方法返回一个 ApiResult 对象。
该方法的主要作用是根据用户 ID 和考试编码查询用户尚未参加的考试信息。在方法中首先使用了 Optional 类的 ofNullable() 方法判断 userId 和 examCode 是否为 null,如果为 null,则抛出一个自定义的 BusinessCodeException 异常,异常信息为 "SELECT_ERROR_CODE"。
然后调用了 examManageService 的 findNotHadJoinedExamByUserId 方法,该方法会根据用户 ID 和考试编码查询用户尚未参加的考试信息,并返回 ExamManageEntity 对象。
最后,根据查询结果判断是否有查询到考试信息,如果为 null,则返回一个错误的 ApiResult 对象,否则返回一个成功的 ApiResult 对象,携带查询到的考试信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)