下面是我 AJAX 请求:
$(document).on('click', '.internship-form-submit-btn', function(e) {
e.preventDefault();
var baseUrl = "localhost:8080/devhub";
var formClass = $(this).closest('form').attr('class');
var formSelector = $('.' + formClass);
var formData = formSelector.serialize();
var url = baseUrl + formSelector.data('action');
$.ajax({
type: 'POST',
url: url,
data: formData,
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
console.log('Error:', error);
}
});
})
在Spring Boot
中,我使用@CrossOrigin
注解:
@RestController
@RequestMapping(value = "/internship")
@CrossOrigin ("*")
public class InternshipController {
@PostMapping("/web-development")
@ResponseBody
public String postWebDevelopmentForm(@RequestBody WebDevelopmentDTO webDevelopmentDTO) {
System.out.println("1");
return "career-mentorship.html";
}
}
但是在Chrome
浏览器控制台上,我得到:
Access to XMLHttpRequest at 'localhost:8080/devhub/internship/web-development?firstName=&lastName=&email=&experience=&coverLetter=' from origin 'http://127.0.0.1:5501' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
我参考了很多类似的帖子,但都没有帮助。我正在tomcat v9.0
上运行我的应用程序
1条答案
按热度按时间7gcisfzg1#
基本URL应以HTTP方案开头: