[英]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.