weixin_33704591 2017-05-25 09:27 采纳率: 0%
浏览 8

通过Ajax的春季休息电话

I am stuck with a problem when i send POST request through ajax to Spring Rest i am getting following error. But it is working fine with Postman tool.

How to solve this? Please help.

enter image description here

@RestController
public class LoginController {

    @Autowired
    LoginServiceBo loginServiceBo;

    public void setLoginServiceBo(LoginServiceBo loginServiceBo) {
        this.loginServiceBo = loginServiceBo;
    }

    @RequestMapping(value="/login",method = RequestMethod.GET)
    public ModelAndView redirectLoginForm() {
        System.out.println("Login Page...");
        return new ModelAndView("login");
    }

    @RequestMapping(value="/login",method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Login> login(@RequestBody Login login) {
        System.out.println("Checking Login Credentials...");
        boolean isValid=loginServiceBo.checkUserLogin(login);

        if(isValid) {
            System.out.println("Found...");
            return new ResponseEntity<Login>(login, HttpStatus.ACCEPTED);
        }
        System.out.println("Not Found...");
        return new ResponseEntity<>(login, HttpStatus.NOT_FOUND);   
    }

}

In javascript

function RestPost() {
userId = document.getElementById("userId");
password = document.getElementById("password");
var json = {
    "userId": userId,
    "password": password
};

$.ajax({
    url: prefix + "/login",
    type: 'POST',
    data: JSON.stringify(json),
    cache: false,
    dataType: 'json',
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Accept", "application/json");
        xhr.setRequestHeader("Content-Type", "application/json");
    },
    success: function(response) {
        alert(JSON.stringify(response));


    },
    error: function(jqXhr, textStatus, errorThrown) {
        alert(jqXhr.status + ' ' + jqXhr.responseText);


    }
});
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
    • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题