javascript - 如何使用原生JS实现类似jQuery的getJSON功能(跨域)?
黄舟
黄舟 2017-04-10 14:28:45
[JavaScript讨论组]

写如下代码:

        request = new XMLHttpRequest();
        request.open('GET', url, true);

        request.onreadystatechange = function() {
          if (request.status >= 200 && request.status < 400){
            // Success!
            data = JSON.parse(request.responseText);
            console.log(data);
            if(data.ret == 1){
                window.location = data.url;

            }
          } 
        };

        request.send();

发现会提示:
XMLHttpRequest cannot load http://XXXXX.com/app.php?callback=cb. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.YYYY.com' is therefore not allowed access.

如何实现类似getJSON的JSONP跨域呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(1)
天蓬老师

cc http://segmentfault.com/q/1010000000513202#a-1020000000513237

动态加载script DOM的代码:

var s = document.createElement('script');
s.src = "http://xxxx.com/api.php?callback=sdffdf";
document.body.append(s)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号