iframe嵌套微信公众号文章

function getUrl(URL) {
                let http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
                //调用跨域API
                let realurl = http + '//cors-anywhere.herokuapp.com/' + URL;
                $.ajax({
                    type: 'get',
                    url: realurl,
                    success: function (response) {
                        if (response) {
                            let html = response;
                    html = html.replace(/data-src/g, "src")
                        .replace(/)<[^<]*)*<\/script>/g, '')
                        .replace(/https/g, 'http');
                    // let html_src = 'data:text/html;charset=utf-8,' + html;
                    let html_src = html;
                    let iframe = document.getElementById('iFrame');
                    iframe.src = html_src;
                    var doc = iframe.contentDocument || iframe.document;
                    doc.write(html_src);
                    doc.getElementById("js_content").style.visibility = "visible";
                        }
                    },
                    error: function (err) {
                        console.log(err);
                        alert('好像出错了...');
                        
                    }
                });
                
            }
            //调用getUrl方法,url为微信公众号文章链接
            var url = "https://mp.weixin.qq.com/s/m6DZ4mPkPfpmLnUC9upO1A";
            getUrl(url);





 // // 方法二
            // $.ajaxPrefilter( function (options) {
            //     if (options.crossDomain && jQuery.support.cors) {
            //         var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
            //         options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
            //     };
            // });

            // var share_link = "https://mp.weixin.qq.com/s/m6DZ4mPkPfpmLnUC9upO1A";    //这里是微信文章地址

            // $.get( share_link, function (response){
            //     var html = response;
            //     html=html.replace(/data-src/g, "src");
            //     var html_src = 'data:text/html;charset=utf-8,' + html;
            //     $("#iFrame").attr("src" , html_src);
            // });

注意:这两种方案都只能将文字回显出来,但是图片还是不能显示。暂时还没找到解决方案

你可能感兴趣的:(js,H5)