当站点发送但不通过邮递员发送时,Spring Boot 会拒绝正文

我正在使用 Spring Boot,但发生了一些奇怪的事情。我想向我的 springboot 服务器发出发布请求,当我通过邮递员执行此操作时我成功,但当我通过我的网站执行此操作时失败。我尝试将其更改为不同的 HTTP 请求和数据模型,但出现相同的错误。我送来的尸体和我亲眼所见、测试过的似乎并没有什么不同。错误堆栈跟踪位于 Web 请求中(一直向下)。


我的控制器代码


    @CrossOrigin(maxAge = 3600)

    @RequestMapping(value = "/auth", method = RequestMethod.POST)

    @ResponseBody

public ResponseEntity<?> authenticate(@RequestBody Map<String, String> body) {

    System.out.println(body);

    ResponseModel responseModel;

    ProfileResource login = new ProfileResource();

    login.setUsername(body.get("Username"));

    login.setPassword(body.get("Password"));


    // other code..


    responseModel.setData(login);

    return new ResponseEntity<>(responseModel, HttpStatus.ACCEPTED);

}

我的JS代码:


$(document).ready(function() {

$("#LoginButtonID").click(function(){

    if($('#LoginButtonID').is(':visible')) {

        var link  = "http://localhost:9024/login/auth";

        var body = "{"+

            "\"Username\":\""+document.getElementById("UserNameID").value+"\", " +

            "\"Password\":\""+document.getElementById("PasswordID").value+"\"" +

            "}";

        console.log(body);

        sendRequest(link,'POST',body);

        console.log(data)

        if(data.response.toString()===("valid and successful")){

            localStorage.setItem("username",document.getElementById("UserNameID").value);

            window.location.href = "../html/UserPages/Welcome.html";

        }else if(data.response.toString()===("failed to authenticate")){

            alert("failed to login");

        }

    }

})

});

人到中年有点甜
浏览 93回答 2
2回答

汪汪一只猫

查看您的响应 JSON 是否包含该response字段。根据日志,收到的响应是{"Username":"a", "Password":"a"}在您正在执行的 JS 代码中data.response.toString(),因为响应未定义。你收到Uncaught TypeError: Cannot read property 'response' of undefined错误了。我尝试了以下代码,它在我的系统上运行:$(document).ready(function() {&nbsp; &nbsp; $("#LoginButtonID").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link&nbsp; = "http://localhost:9024/login/auth";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var body = "{"+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "\"Username\":\""+document.getElementById("UserNameID").value+"\", " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "\"Password\":\""+document.getElementById("PasswordID").value+"\"" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendRequest(link,'POST',body);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data.response.toString()===("valid and successful")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem("username",document.getElementById("UserNameID").value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("done!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if(data.response.toString()===("failed to authenticate")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("failed to login");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })});function sendRequest(link, type, body) {&nbsp; &nbsp; var xhr = new XMLHttpRequest();&nbsp; &nbsp; xhr.open(type, link, false);&nbsp; &nbsp; xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');&nbsp; &nbsp; xhr.onreadystatechange = function () {&nbsp; &nbsp; &nbsp; &nbsp; if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 202 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = JSON.parse(xhr.responseText);&nbsp; &nbsp; &nbsp; &nbsp; }else if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 401 ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = JSON.parse(xhr.responseText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; xhr.send(body);}控制器代码:@CrossOrigin(maxAge = 3600)@PostMapping("auth")@ResponseBodypublic ResponseEntity<?> authenticate(@RequestBody Map<String, String> body) {&nbsp; &nbsp; // sending some response for the sake of testing&nbsp; &nbsp; body.put ("response","valid and successful");&nbsp; &nbsp; return new ResponseEntity<>(body, HttpStatus.ACCEPTED);}

叮当猫咪

您需要使用异步请求。您已经在使用 jquery,它在$.ajax()中提供了此功能,无需使用 javascript 的内置 XMLHttpRequest。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java