Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSONP不工作的跨域:权限问题_Php_Jquery_Ajax - Fatal编程技术网

Php JSONP不工作的跨域:权限问题

Php JSONP不工作的跨域:权限问题,php,jquery,ajax,Php,Jquery,Ajax,我试图在我的网站上显示一些电影海报,方法是从不同的网站加载它们。我抓取了海报的URL并将它们保存到mySQL数据库中。当用户查找海报时,脚本将搜索数据库并通过ajax获取完整URL,并填充IMG标记以显示图片 我注意到,我从两个网站中获得链接的其中一个网站返回“您没有访问…的权限”。但是如果我在浏览器地址栏中复制相同的链接,我可以看到图片。经过一些研究我猜这是由于跨域问题。我了解到最常见的解决方案是JSONP,因此我设法编写了以下代码。您可能会看到有两个链接:第一个链接有效,第二个链接(film

我试图在我的网站上显示一些电影海报,方法是从不同的网站加载它们。我抓取了海报的URL并将它们保存到mySQL数据库中。当用户查找海报时,脚本将搜索数据库并通过ajax获取完整URL,并填充IMG标记以显示图片

我注意到,我从两个网站中获得链接的其中一个网站返回“您没有访问…的权限”。但是如果我在浏览器地址栏中复制相同的链接,我可以看到图片。经过一些研究我猜这是由于跨域问题。我了解到最常见的解决方案是JSONP,因此我设法编写了以下代码。您可能会看到有两个链接:第一个链接有效,第二个链接(filmup.it)返回权限错误。(问题不是由于缺少“www”)

HTML代码

<input type="text" id="link" value="http://www.cinemadelsilenzio.it/images/film/poster/11469_big.jpg" />
<!-- <input type="text" id="link" value="http://filmup.leonardo.it/posters/loc/500/dieanotherday.jpg" /> -->
<button class="searchbutton" type="button">Vai</button>

<br />

<img id="poster" src=""/>


<script type="text/javascript">
$(document).ready(function() {
    $(".searchbutton").click(function(){    
        var _text = document.getElementById("link").value;
        $.ajax({
            url: 'img.php',
            data: {parameter1: _text},
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'jsonpCallback',
            success: function(){
                alert("success");
            }
        });
    }); 
});

function jsonpCallback(data){
    $("#poster").attr("src", data);
}
</script>
// let's suppose that here there's a mySQL code to retrieve the links
if (isset($_GET['parameter1']) && isset($_GET['callback'])) {
    $picture = $_GET['parameter1'];
    echo $_GET['callback']."(".json_encode($picture).");";
}

解决了
$(".searchbutton").click(function(){    
    var _text = document.getElementById("link").value;
    $.get("img.php", {parameter1: _text }, 
        function(data) {
            $('#poster').html('<img src="data:image/jpg;base64,' + data + '" />');
        }
    );
});
<?php
    header('Content-type: image/jpeg');
    echo base64_encode(file_get_contents($_GET["parameter1"]));
?>
我不知道为什么,但是
file\u get\u contents
没有使用从数据库检索到的数据完成,尽管
$row[“Link”]
有正确的内容。看起来,
$row[“Link”]
只是空的,但不是空的

您需要将输出重定向到其他页面。改变

echo base64_encode(file_get_contents($row["Link"]));

image.php文件将是:

header('Content-type: image/jpeg');
echo base64_encode(file_get_contents($_GET["link"]));

希望这对某人有所帮助。

为什么你不能直接将img
.src
设置为链接值?@Jack。请解释。我的意思是单击处理程序中的
$('#poster').prop('src',_text)
,而不是
$.ajax()
。以上代码已简化。如前所述,我将链接保存在数据库中,通过用户搜索检索并通过ajax显示到我的网站中。一个链接起作用,另一个链接出现权限错误。我不明白为什么会出现权限错误,因为
img.php
是相同的路径,所以除非脚本有问题,否则问题可能在其他地方?
header("location: image.php?link=".$row["Link"]);
header('Content-type: image/jpeg');
echo base64_encode(file_get_contents($_GET["link"]));