简体   繁体   English

使用JSONP和PHP调用外部html文件

[英]Calling an external html file using JSONP and PHP

It is possible this question might be a little vague for the liking of many people here, but my knowledge of the topic is vague also, and despite some searching I have not been able to find anywhere online with clear instructions on how to do this although (in my view) it should probably be one of the simpler cross-domain JSONP operations. 这个问题可能对这里的许多人来说有点模糊,但是我对该主题的了解也很模糊,尽管进行了一些搜索,但是我仍然无法在网上找到任何有关如何执行此操作的明确说明(在我看来),它应该是更简单的跨域JSONP操作之一。

I will explain what I am trying to do. 我将解释我要做什么。 I am trying to retrieve the contents of a HTML file on an external server using both JSONP and a PHP script. 我正在尝试使用JSONP和PHP脚本检索外部服务器上HTML文件的内容。 I have access to the server - my PHP script is as follows: 我可以访问服务器-我的PHP脚本如下:

    <?php
    $callback = '';
    if (isset($_GET['callback']))
    {
    $callback = filter_var($_GET['callback'], FILTER_SANITIZE_STRING);
    }
    $html = include($callback);
    echo $callback . '('.json_encode($html).');';
    ?>

Using this it seems that when I type www.myurl.com/myscript.php?callback=filename.html , the correct file is displayed (although, for some reason, the ?callback is duplicated in the address, and the filename is appended to the end of the output displayed... 使用此方法,当我键入www.myurl.com/myscript.php?callback=filename.html ,似乎显示了正确的文件(尽管由于某种原因,地址中的?callback是重复的,并附加了文件名)到显示的输出末尾...

I have tried a few different scripts for my HTML file but currently have the following... 我为我的HTML文件尝试了几种不同的脚本,但目前具有以下功能...

    function request_jsonp(){
    var script = document.createElement('script');
    var url = 'http://www.myurl.com/myscript.php?callback=filename.html';
    script.setAttribute('src', url);
    document.getElementsByTagName('head')[0].appendChild(script);
    }

Now obviously this doesn't work because the output of the PHP script isn't a working function of any sort, but I am not sure of the best way to go about making it one. 现在显然这是行不通的,因为PHP脚本的输出不是任何形式的工作函数,但是我不确定使它成为最佳方法的最佳方法。 I would like to somehow wrap the output in something like this: 我想以某种方式包装输出:

    function DisplayOutput() {
        document.getElementById('output').innerHTML = ...external HTML goes here!... ;
    }

...but currently I'm really not sure of the best way to do this. ...但是目前我真的不确定执行此操作的最佳方法。

Any help would be very much appreciated. 任何帮助将不胜感激。

callback needs to be the name of the callback function. callback必须是回调函数的名称。 You should generate a unique one each time the function is called. 每次调用该函数时,您都应该生成一个唯一的。

You should use a second, different query string variable to determine what URI to fetch. 您应该使用另一个不同的查询字符串变量来确定要获取的URI。

var jsonp_counter = 0;

function request_jsonp(url, callback){
    jsonp_counter++;
    var callback_name = "jsonp_function_" + jsonp_counter;
    window[callback_name] = callback;

    var jsonp_url = 'http://www.myurl.com/myscript.php" +
              "?callback=" + encodeURIComponent(callback_name) +
              "&url=" + encodeURIComponent(url);

    var script = document.createElement('script');
    script.setAttribute('src', url);
    document.getElementsByTagName('head')[0].appendChild(script);
}

request_jsonp("filename.html", function (data) {
     do_something_with(data);
});

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

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