JavaScript手动实现JSONP代码

浏览器的同源策略不允许我们直接通过ajax call别的域名上的url,但是script,img标签却米有这个限制,jsonp便是利用了这一点,通过在页面上生成一个src为我们所要调用url的script标签,能拿到服务器返回的结果,当然这个只支持get请求,同样,服务器端也要能够接受我们所传递的参数。

 

下面是一个例子:

<script>
function removeScript(scriptId) {
    const script = document.getElementById(scriptId);
    document.getElementsByTagName('head')[0].removeChild(script);
}

function clearFunction(functionName) {
    try {
        delete window[functionName];
    } catch (e) {
        window[functionName] = undefined;
    }
}

function call(url) {
    return new Promise(function(resolve, reject) {
        var callbackFunction = 'callbackFunc' + Math.ceil(Math.random() * 100000000000000000);
        url += (url.indexOf('?') === -1) ? '?' : '&';
        const jsonpScript = document.createElement('script');
        jsonpScript.setAttribute('src', `${url}callback=${callbackFunction}`);
        scriptId = callbackFunction;
        jsonpScript.id = scriptId;
        document.getElementsByTagName('head')[0].appendChild(jsonpScript);

        window[callbackFunction] = function(data) {
            console.log('--------------------------------');
            removeScript(scriptId);
            clearFunction(callbackFunction);
            resolve(data);
        }
    });
}

//这里我后台用的sails,返回的类似于这样子:callbackFunc20461582336288720({"a":'aa','b':'bb'})
call("http://localhost:1337/test/test1").then(function(data) {
    console.log(data);
}).catch(function(error) {
    console.log(error);
})

</script>

这里用了Promise,只处理了正常resolve的,以后也可以加reject弄个超时处理之类,这里需要注意的是在完成方法的调用后,一定要记得将script remove掉,window上挂载的方法也要remove掉。

转载于:https://www.cnblogs.com/gogolee/p/6439664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值