springbootajaxhas been blocked by CORS policy: No ‘Access-Control-Allow-Origin
生活随笔
收集整理的這篇文章主要介紹了
springbootajaxhas been blocked by CORS policy: No ‘Access-Control-Allow-Origin
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
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解決跨域的問題的兩種方法
前端測試代碼:
<!DOCTYPE html> <html><head><meta charset="UTF-8"><title></title> </head><body> <div id="form-div"><form id="form1"><p>用戶名:<input name="email" type="text" id="txtUserName" tabindex="1" size="15" value="" /></p><p>密 碼:<input name="password" type="text" id="TextBox2" tabindex="2" size="16" value="" /></p><p><input type="button" value="登錄" onclick="login()"> <input type="reset" value="重置"></p></form> </div> </body> <script type="text/javascript" src="../static/jquery/jquery-3.3.1.js"></script> <script type="text/javascript">function login() {$.ajax({//幾個參數需要注意一下type: "POST", //方法類型dataType: "json", //預期服務器返回的數據類型url: "http://localhost:8080/user/login1", //urldata: $('#form1').serialize(),success: function(result) {console.log(result); //打印服務端返回的數據(調試用)if(200 == result.resultCode) {alert("SUCCESS");};},error: function() {alert("異常!");}});} </script></html>?第一種:寫一個class,配置的class
package com.example.demo;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter;/*** Author:Yangjingcheng* Date:2018/*/ @Configuration public class CorsConfig {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedOrigin("*");corsConfiguration.addAllowedHeader("*");corsConfiguration.addAllowedMethod("*");return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();// 配置所有請求source.registerCorsConfiguration("/**", buildConfig());return new CorsFilter(source);} }第二種,在你要訪問的Controller的方法上面加上注解?@CrossOrigin
package com.example.demo;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.List;@Controller public class TestController {@CrossOrigin@RequestMapping("/user/login1")@ResponseBodypublic List<User> userLogin(User user) {System.out.println(user);ArrayList<User> users = new ArrayList<>();for (int i = 0; i < 3; i++) {users.add(user);}return users;} }OK了
?
?轉自:https://blog.csdn.net/lovePaul77/article/details/85681404
?
總結
以上是生活随笔為你收集整理的springbootajaxhas been blocked by CORS policy: No ‘Access-Control-Allow-Origin的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在c#中将debug/release下文
- 下一篇: C#-文件只读时进行拷贝或删除报错, 对