简体   繁体   English

从本地 .html 文件向 localhost:8080 发送 AJAX POST 请求时出错。 CORS:“它没有 HTTP ok 状态。”

[英]Error sending AJAX POST request from local .html file to localhost:8080. CORS: "It does not have HTTP ok status."

I am getting an error when attempting to send a POST via AJAX from my own .html and .js files to localhost:8080.尝试通过 AJAX 从我自己的 .html 和 .js 文件向 localhost:8080 发送 POST 时出现错误。 Upon submitting the request, the full error reads: "Access to XMLHttpRequest at ' http://localhost:8080/contact/new-message ' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."提交请求后,完整错误显示为:“访问 XMLHttpRequest at ' http://localhost:8080/contact/new-message ' from origin 'null' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态。”

CORS is already enabled on my browser, so access is automatically "Access-Control-Allow-Origin : *", so this is a different from that error. CORS 已在我的浏览器上启用,因此访问自动为“Access-Control-Allow-Origin : *”,因此这与该错误不同。

Is there a way to include an "ok" status in the header?有没有办法在标题中包含“ok”状态? Or is the problem arising from elsewhere?还是问题出在别处? Any help is greatly appreciated.任何帮助是极大的赞赏。 Here are some code snippets:下面是一些代码片段:

My JavaScript, which runs as part of a form-submission:我的 JavaScript,作为表单提交的一部分运行:

function submitMessageAJAXCall(inputName, inputEmail, inputMessage, inputRegion) {
    $.ajax({
        type: 'POST',
        url: 'http://localhost:8080/contact/new-message',
        data: JSON.stringify({
            rbName: inputName,
            rbEmail: inputEmail,
            rbMessageText: inputMessage,
            rbRegionId: inputRegion
        }),
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        success: function() {

            alert('Success!');
            displayThankYouMessage();

        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('Unfortunately that message did not go through.');
        }
    }); 
}

The Java code which recieves it:接收它的Java代码:

@PostMapping("/new-message")
    private ResponseEntity<HttpStatus> addNewMessage(@RequestBody RBNewMessage rbNewMessage) {
        //validate message in service layer
        boolean isRequestValid = contactService.validateNewMessageRB(rbNewMessage);

        //is message is good, save it; else, return an error
        if (isRequestValid == true) {
            //create a new message
            ContactMessage message = new ContactMessage();

            //set message fields
            message.setMyName(rbNewMessage.getRbName());

            message.setMyEmail(rbNewMessage.getRbEmail());

            message.setMessageText(rbNewMessage.getRbMessageText());

            LocalDateTime timeOfMessage = LocalDateTime.now();
            LocalDateTime timeWithoutNano = timeOfMessage.withNano(0);
            message.setTimeStamp(timeWithoutNano);

            int regionId = rbNewMessage.getRbRegionId();
            Region region = regionService.getRegionById(regionId);
            message.setRegion(region);

            ContactStatus cs = contactStatService.getStatusById(1);     
            message.setContactStatus(cs);

            //save message
            contactService.save(message);

            //return success
            return new ResponseEntity<>(HttpStatus.OK);

        } else {
            //return error
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

And this is an example of a Postman request that is successful:这是一个成功的 Postman 请求示例:

{
    "rbName": "John Johnson",
    "rbEmail" : "JohnJohnson@Email.com",
    "rbMessageText" : "Hello there, this is my message.",
    "rbRegionId" : 5
}

将@CrossOrigin 注释(导入 org.springframework.web.bind.annotation.CrossOrigin)添加到处理请求的控制器类的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS 错误:它没有 HTTP 正常状态 - CORS Error: It does not have HTTP ok status Cors 策略错误:它没有 HTTP 正常状态 - Cors Policity Error: It does not have HTTP ok status 如何允许 CORS 与 Flask 获得对预检请求的响应没有 http 正常状态 - How to allow CORS with Flask getting Response to preflight request does not have http ok status ERROR-:8080/:1 GET http://localhost:8080/ 404 (OK) - ERROR- :8080/:1 GET http://localhost:8080/ 404 (OK) 如何在Socket.io中修复cors策略问题“它没有HTTP ok状态” - How to fix cors policy problem “It does not have HTTP ok status” in Socket.io 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it AJAX Https 请求显示 CORS 错误。 (来自本地服务器) - AJAX Https request showing CORS error. (From local server) Ajax的本地主机CORS错误 - ajax localhost cors error 预检请求未通过访问控制检查:它没有 HTTP ok 状态 - Preflight request doesn't pass access control check: It does not have HTTP ok status 将带有JSON数据的POST请求从HTML发送到Java Rest Web服务时出现HTTP错误415 - HTTP Error 415 when sending POST request with JSON data from HTML to Java Rest web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM