简体   繁体   English

跨域请求-执行所有脚本的页面内容

[英]Cross Domain Request - page content with all scripts executed

The title describes my problem quite good. 标题很好地描述了我的问题。

I try to do a cross domain request, where the loaded content is executed first. 我尝试执行跨域请求,在该请求中首先执行加载的内容。

So I have page www.xyz.com which loads some page content via ajax (for example a live ticker) and I'd like to use this content in my own file. 因此,我有www.xyz.com页面,该页面通过ajax加载了一些页面内容(例如实时行情自动收录器),我想在自己的文件中使用此内容。 Since Php can't execute anything I'd like to do a jquery get request but I can't figure out how to get both at the same time, cross domain request and executed javascript. 由于Php无法执行任何操作,因此我想执行jquery get请求,但无法弄清楚如何同时获取两者,跨域请求和执行的javascript。

Thanks in advance. 提前致谢。

Update: 更新:

For example I tried this code: 例如,我尝试了以下代码:

function requestCrossDomain( site, callback ) {

    if ( !site ) {
        alert('No site was passed.');
        return false;
    }

    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';

    $.getJSON( yql, cbFunc );

    function cbFunc(data) {

    if ( data.results[0] ) {

        data = data.results[0];

        if ( typeof callback === 'function') {
            callback(data);
        }
    }
    else throw new Error('Nothing returned from getJSON.');
    }
}

requestCrossDomain('http://www.google.com', function(results) {
    //htmlcode = eval(results); 
    htmlcode = results; 
    $('#container').html(htmlcode);
});

If you absolutely must use AJAX to grab a file, then try to execute it, you can do so with the following code: 如果您绝对必须使用AJAX来抓取文件,然后尝试执行该文件,则可以使用以下代码进行操作:

eval(ajaxResponseText);

Another way to do this is to generate a tag from an XHR taking advantage of the data URI scheme. 执行此操作的另一种方法是利用data URI方案从XHR生成标签。

var script = document.createElement("script")
script.src = "data:text/javascript;charset=utf8," + encodeURIComponent(ajaxResponseText);
document.body.appendChild(script);

EDIT 编辑

Now that there is an example illuminating your question, I must amend my answer. 现在有一个例子可以说明您的问题,我必须修改答案。

You cannot make a Cross-Origin request using ajax on just about all browsers without abiding by the CORS specification because of the prevalence and severity of cross-origin attacks. 由于跨域攻击的普遍性和严重性,您不能在几乎所有浏览器上都使用ajax进行跨域请求而不遵守CORS规范。 The Cross Origin Resource Sharing specification allows you to make ajax requests to a different origin so long as the new origin permits it. 跨源资源共享规范允许您向其他源发出ajax请求,只要新源允许。 If it is not working, the javascript console might be a good place to look for errors. 如果无法正常使用,则JavaScript控制台可能是查找错误的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM