亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當站點發送但不通過郵遞員發送時,Spring Boot 會拒絕正文

當站點發送但不通過郵遞員發送時,Spring Boot 會拒絕正文

人到中年有點甜 2023-09-27 16:58:41
我正在使用 Spring Boot,但發生了一些奇怪的事情。我想向我的 springboot 服務器發出發布請求,當我通過郵遞員執行此操作時我成功,但當我通過我的網站執行此操作時失敗。我嘗試將其更改為不同的 HTTP 請求和數據模型,但出現相同的錯誤。我送來的尸體和我親眼所見、測試過的似乎并沒有什么不同。錯誤堆棧跟蹤位于 Web 請求中(一直向下)。我的控制器代碼    @CrossOrigin(maxAge = 3600)    @RequestMapping(value = "/auth", method = RequestMethod.POST)    @ResponseBodypublic 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");        }    }})});
查看完整描述

2 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

查看您的響應 JSON 是否包含該response字段。


根據日志,收到的響應是{"Username":"a", "Password":"a"}在您正在執行的 JS 代碼中data.response.toString(),因為響應未定義。你收到Uncaught TypeError: Cannot read property 'response' of undefined錯誤了。


我嘗試了以下代碼,它在我的系統上運行:


$(document).ready(function() {

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

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

            var body = "{"+

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

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

                "}";

            sendRequest(link,'POST',body);


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

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

                alert("done!")

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

                alert("failed to login");

            }

    })

});


function sendRequest(link, type, body) {

    var xhr = new XMLHttpRequest();

    xhr.open(type, link, false);

    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');


    xhr.onreadystatechange = function () {

        if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 202 ) {

            data = JSON.parse(xhr.responseText);

        }else if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 401 ){

            data = JSON.parse(xhr.responseText);            }

    }

    xhr.send(body);

}

控制器代碼:


@CrossOrigin(maxAge = 3600)

@PostMapping("auth")

@ResponseBody

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

    // sending some response for the sake of testing

    body.put ("response","valid and successful");

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

}


查看完整回答
反對 回復 2023-09-27
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您需要使用異步請求。您已經在使用 jquery,它在$.ajax()中提供了此功能,無需使用 javascript 的內置 XMLHttpRequest。



查看完整回答
反對 回復 2023-09-27
  • 2 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號