Ajax跨域請求時出現Access to XMLHttpRequest at 'xxx' from origin 'xxx' has been been blocked by CORS policy

Ajax請求報錯,出現如下錯誤:
Access to XMLHttpRequest at 'localhost:8888/register' from origin 'http://localhost:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

CORS策略已阻止從源“http://localhost:8080”訪問“localhost:8888/register”處的xmlhttprequest:跨源請求僅支持協議方案:http、data、chrome、chrome擴展、https。

解決辦法:在請求路徑中加上http://
http://localhost:8888/register


以下附上我自己解決跨域問題時找到的一些方法:

後臺解決跨域問題

SpringBoot:加上@CrossOrigin註解

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@CrossOrigin
@RestController
@RequestMapping("test")
public class TestController {
 
    @RequestMapping("/one")
	public Object one(HttpServletRequest request){
		System.out.println("請求成功");
        return "請求成功";
	}
 
 
    ....
 
 
}

詳見《處理 No ‘Access-Control-Allow-Origin’ header is present on the requested resource 問題》

PHP、JAVA、.NET、Node.js、SpringBoot《ajax跨域,這應該是最全的解決方案了》

前端解決跨域問題

前端解決跨域問題的8種方案(最新最全)

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