Springboot 利用CORS 解决跨域问题

Springboot 利用CORS 解决跨域问题,第1张

Springboot 利用CORS 解决跨域问题 什么是跨域

首先我们先用springboot 建立1个简单的API, 它返回1个json

package com.example.demo_api_cors.controller;

import com.example.demo_api_cors.dto.ValueDto;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {

    @RequestMapping("/getValue")
    @ResponseBody
    public ValueDto cors1(){
        return new ValueDto(0, "new Value");
    }
}

然后启动这个springboot, 端口是8081

可以看到, 我们可以在浏览器地址栏输入 url http://localhost:8081/getValue
来访问这个api, 其中localhost 就是域名, 8081就是端口

打开debug mode, 可以见到Response Header的信息, 如上图篮圈中。

然后我们在建立1个springboot 项目, 使用8082 这个端口, 尝试在项目中的某个html文件调用上面这个api




    

    
    
        
            value is {{value}}					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4655966.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存