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种方案(最新最全)

你可能感兴趣的:(Ajax)