Ajax调用Php脚本以获取base64字符串中的图像,该字符串可在本地主机上工作,但不能从托管服务器中获取


Ajax call to Php script to fetch image in base64 string working on localhost but not from hosting server

描述:我有一个HTML页面,它会在加载时触发Ajax调用。在这个Ajax调用中,我传入了id(与服务器上的图像名称相同)。每个通过Ajax函数发送到php脚本的id都会获取图像,然后php脚本会将图像转换为base64并返回给Ajax调用。成功后,此Javascript函数将base64字符串写入(href="base64")相应的id。

问题:现在所有这些都在localhost上运行良好,.htaccess文件中有指令,但就在我将其放在托管服务器上时,HTML页面正在对PHP脚本进行ajax调用,但PHP脚本没有像在localhost中那样返回base64字符串,而是返回Index.HTML的标记"#将所有请求重定向到index.html"(但这只是为了避免用户发出任何不需要的请求)。

执行的检查:1)名称区分大小写。2) 已将文件放置在正确的目录位置。3) 将本地主机上的请求(使用Developers工具,Network选项卡)与托管服务器上的请求进行比较,两者都与"Status 200"相同(其内容分别为"base64string"answers"markups of index.html")。

HTML

<a href="#" class="example-image-link" id="travel_1"></a>
<a href="#" class="example-image-link" id="travel_2"></a>

JavaScript

<script type="text/javascript">
$('.example-image-link').each(function() {
var id = $(this).attr("id");
var data = id;
$.ajax({
type: "POST",
url: 'http://mysite.in/home/myname/public_html/image_extract.php',
async: true,
data: {post: data},
success: function(data) {
var x = "data:image/jpeg;base64,";
var y = data;
 z = x + y;  
document.getElementById(id).href= z; 
    return false;
}
});
});
</script>

image_extract.php

$q = $_POST['post'];
$main = explode("_", $q);
if($main[0] == "travel")
{
$dir = "images/travel_pics/".$q.".jpg";
$image = file_get_contents($dir);
$imdata = base64_encode($image);
    if ($imdata !== false) {
        echo $imdata;
    }
    else {
        echo 'An error occurred.';
    }
}

.HTACESS

# Allows ModRewrite to work
Options FollowSymLinks
# Turn on rewrite engine
RewriteEngine On
RewriteBase /
# Redirect all requests to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html
IndexIgnore *
RewriteCond %{HTTP_REFERER} !^http://mysite.in/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://mysite.in$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mysite.in/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mysite.in$      [NC]
RewriteRule .*'.(jpg|jpeg|gif|png|bmp)$ - [F,NC]

为什么我在托管服务器(Godd*dy)上没有收到来自php脚本的base64字符串?提前感谢。:)

.htaccess看起来不错,JS也不错——似乎php文件image_extract不存在。

您正在使用路径http://mysite.in/home/myname/public_html/image_extract.php

尝试http://mysite.in/image_extract.php,因为/home/myname/public_html是您的文档根(我想)。