如何解决JS|axios调用LiveGBS(GB28181国标流媒体服务)接口时遇到的跨域问题

79 阅读1分钟

GB28181流媒体服务搭建

搭建入口,解压启动即用:www.liveqing.com/docs/downlo…

JS调用遇到跨域解决示例

添加 xhrFields: { withCredentials: true},crossDomain: true,

$.ajax({
    type: "GET",
    url: "http://other-domain:10000/api/v1/login",
    xhrFields: { 
        withCredentials: true
    },
    crossDomain: true, 
    data: {
        username: 'test',
        password: 'test'
    },
    success: function(data) {
        console.log(data);
    }
})

axios请求接口遇到跨域问题

添加 withCredentials: true

post请求

 axios.post("http://ip:10000/api/v1/test", {
            param1: 'test',
            param2: 'test'
        }, {
            withCredentials: true
        }).then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err);
        })

get请求

   axios.get("http://ip:10000/api/v1/test", {
            params:{start:0,limit:10},
            withCredentials: true
        }).then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err);
        })