springboot&ajax&has been blocked by CORS policy: No ‘Access-Control-Allow-Origin

骑猪看日落 2022-02-01 11:23 359阅读 0赞

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

ajax+springboot解决跨域问题,以下报的错误就是html跨域的问题

Access to XMLHttpRequest at ‘http://localhost:8080/user/login1‘ from origin ‘http://localhost:59033‘ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

springboot解决跨域的问题的两种方法

前端测试代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div id="form-div">
  9. <form id="form1">
  10. <p>用户名:<input name="email" type="text" id="txtUserName" tabindex="1" size="15" value="" /></p>
  11. <p>密 码:<input name="password" type="text" id="TextBox2" tabindex="2" size="16" value="" /></p>
  12. <p><input type="button" value="登录" onclick="login()"> <input type="reset" value="重置"></p>
  13. </form>
  14. </div>
  15. </body>
  16. <script type="text/javascript" src="../static/jquery/jquery-3.3.1.js"></script>
  17. <script type="text/javascript">
  18. function login() {
  19. $.ajax({
  20. //几个参数需要注意一下
  21. type: "POST", //方法类型
  22. dataType: "json", //预期服务器返回的数据类型
  23. url: "http://localhost:8080/user/login1", //url
  24. data: $('#form1').serialize(),
  25. success: function(result) {
  26. console.log(result); //打印服务端返回的数据(调试用)
  27. if(200 == result.resultCode) {
  28. alert("SUCCESS");
  29. };
  30. },
  31. error: function() {
  32. alert("异常!");
  33. }
  34. });
  35. }
  36. </script>
  37. </html>

第一种:写一个class,配置的class

  1. package com.example.demo;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.cors.CorsConfiguration;
  5. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  6. import org.springframework.web.filter.CorsFilter;
  7. /**
  8. * Author:Yangjingcheng
  9. * Date:2018/
  10. */
  11. @Configuration
  12. public class CorsConfig {
  13. private CorsConfiguration buildConfig() {
  14. CorsConfiguration corsConfiguration = new CorsConfiguration();
  15. corsConfiguration.addAllowedOrigin("*");
  16. corsConfiguration.addAllowedHeader("*");
  17. corsConfiguration.addAllowedMethod("*");
  18. return corsConfiguration;
  19. }
  20. @Bean
  21. public CorsFilter corsFilter() {
  22. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  23. // 配置所有请求
  24. source.registerCorsConfiguration("/**", buildConfig());
  25. return new CorsFilter(source);
  26. }
  27. }

第二种,在你要访问的Controller的方法上面加上注解 @CrossOrigin

  1. package com.example.demo;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.*;
  4. import org.springframework.web.servlet.ModelAndView;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. @Controller
  8. public class TestController {
  9. @CrossOrigin
  10. @RequestMapping("/user/login1")
  11. @ResponseBody
  12. public List<User> userLogin(User user) {
  13. System.out.println(user);
  14. ArrayList<User> users = new ArrayList<>();
  15. for (int i = 0; i < 3; i++) {
  16. users.add(user);
  17. }
  18. return users;
  19. }
  20. }

OK了

转自:https://blog.csdn.net/lovePaul77/article/details/85681404

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读