AJAX响应始终为405

时间:2019-03-14 07:23:08

标签: javascript java ajax spring spring-mvc

我有一个AJAX帖子

$.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/rating/save",
        data: JSON.stringify(rating),
        dataType: "json",
        mimeType: "application/json",
        success: function (responseData) {
            console.log(responseData);
            window.location.href = "/welcome"
        },
        error: function (responseData) {
            console.log(responseData);
        }
    });

控制器

@Controller
public class RatingController {
........
    @RequestMapping(value = "/rating/save",method = RequestMethod.POST)
        public ResponseEntity<Object> saveRating(@RequestBody List<RatingDTO> ratingDTO) {
            return new ResponseEntity<>(ratingService.save(ratingDTO),HttpStatus.OK);
        }
}

每次我尝试处理来自控制器的响应,即使我没有异常也

status: 405
statusText: "error"

该错误表明不允许使用该方法,但是从此端点进行的服务效果很好。

1 个答案:

答案 0 :(得分:0)

您应该通过method属性设置POST:

$.ajax({
  method: "POST",
  ...