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跨域,這應該是最全的解決方案了》