解决跨域的方法

1,jsonp

只能发送get请求

利用script发送请求

//指定回调函数为b,在另一个script里面定义回调函数,接受

利用jQuery发送请求

   $(document).ready(function(){
            $.ajax({
            type : 'get',
            url:"http://localhost:3000/test",
            data : {
            },
            cache :false,
            jsonp: "callback",
            jsonpCallback:"success",
            dataType : 'jsonp',
            success:function(data){
                console.log(data);
            },
            error:function(data){
                console.log(data.error);
            }        
        });
    })

2,cors

一帮是后端设置,像nodejs,下载第三方包cors,在app.js页面require进来,在use它,这主要是通过设置 Access-Control-Allow-Origin 来进行的

3,postMessage

发送方使用window.postMessage("Hello World!", "http://example.org");

接受方使用window.addEventListener("message", receiveMessage, false)

function receiveMessage(event){alter('event')}

你可能感兴趣的:(javascript,前端)