ajax/axios調用後端接口跨域問題 has been blocked by CORS policy: No 'Access-Control-Allow-Origin

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

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="登錄" οnclick="login()">&nbsp;<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", //url
            data: $('#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;
    }
 
    @Bean
    public 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")
    @ResponseBody
    public 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;
    }
}
這樣的話就會解決前端接收不到返回值得情況了
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章