gpt4 book ai didi

javascript - php 库无法与 AJAX 配合使用的结果

转载 作者:行者123 更新时间:2023-11-28 06:18:42 24 4
gpt4 key购买 nike

我从index.php调用uberGallery (www.uberGallery.net),如下所示:

<?php

include_once 'resources/UberGallery.php';
include_once 'resources/uberCall.php';
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
...

<link rel="stylesheet" type="text/css" href="resources/themes/uber-naked/style.css" />
<link rel="stylesheet" type="text/css" href="resources/colorbox/5/colorbox.css" />

<script src="//code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="resources/colorbox/jquery.colorbox.js"></script>

</head>
<body>
<div class = "row">
<div class = "col col-lg-12">
<h2 id = "locName"></h2>
</div>
</div>

<div id="gallery">
<?php
$path = "london";
getGallery($path);

$path = "paris";
getGallery($path);

$path = "rome";
getGallery($path);
?>
</div>

<script type="text/javascript" src='js/UX.js'></script>

</body>
</html>

uberCall.php 是这样的:

<?php
include_once ('UberGallery.php');

function getGallery($pathToImage){
UberGallery::init() -> createGallery('locations/'.$pathToImage.'/gallery');
}

if(isset( $_POST['image-path'] )) {

$path = $_POST['image-path'];

UberGallery::init() -> createGallery('locations/'.$path.'/gallery');

}

?>

到目前为止,一切都按预期进行。现在我想从下拉菜单加载画廊并只显示所选的画廊。下拉选择是在 jQuery 脚本中完成的:

$(document).ready(function() {

$("a[rel='colorbox']").colorbox({
maxWidth : "90%",
maxHeight : "90%",
opacity : ".5"
});

$(".dropdown-toggle").dropdown();

$('#demolist li a').click(function(e) {
$('#locationSelector').val($(this).text());
var $this = $(this);
var selLocID = $this.attr("value");

e.preventDefault();

var dataString = {
'location_id' : selLocID
};

$.ajax({
type : 'POST',
dataType : 'json',
url : 'includes/locationAPI.php',
data : dataString,
success : function(data) {
$('#locData').html('');
// erase content if any

$('#locName').html(data['name']);
// from MySQL
// set location headline

$.ajax({
type : "POST",
data : {
'image-path' : data['pfad'] // from MySQL
},
url : 'resources/uberCall.php',
dataType : "html",
async : false,
success : function(data) {
result = data;

alert(result);
$('#gallery').html(result);
}
});

}
});
});

});

当走这条路线时,ubergallery 没有找到任何文件夹,尽管我正在处理与 index.php 中完全相同的 uber.php 。我已经使用 xdebug 查看了 ubergallery.php,它将使用完全相同的参数进行调用。

此外,我尝试在不使用 init::的情况下调用 createGallery(),并在调用 createGallery 之前显式创建 UberGallery 类的新实例。

一切都没有运气。

我在这里做错了什么?

最佳答案

在您的 resources/userCall.php 中脚本您实际上并没有调用图库函数来渲染它。这似乎是第二个 AJAX 请求的目标。该脚本只是定义函数。

编辑:您已经更新了问题中的代码以包含该调用。

假设image-path是您要提供的参数 $pathToImage您需要有一个调用函数 getGallery 的脚本传递 $_POST['image-path'] 中的值.

例如:

<?php
require_once('resources/uberCall.php');

getGallery($_POST['image-path']);

NOTE: Without any filtering sanitisation, bear in mind this could be potentially dangerous as I could pass an image-path parameter that picks a naughty directory.

然后使用该脚本作为第二个 AJAX 调用的目标。

附注删除 async: false在第二个 AJAX 调用中,当请求正在进行时,它会停止页面上的任何其他交互。

请记住,在 resources/userCall.php 中检查参数原始请求位于第二个请求的下一个目录中。这意味着第二个请求的工作目录将不同,路径将解析为 "resources/locations/$path/gallery"而不是"locations/$path/gallery" .

您最好在与原始脚本相同的目录中为 AJAX 操作创建第二个脚本,并且需要 resource/userCall.php按照第一个脚本。或者使用可配置的绝对目录路径来解析图库位置:

<?php
require_once('UberGallery.php');
require_once('config.php');

function getGallery($path) {
UberGallery::init() -> createGallery("{$config['gallery_base_path']}/$path/gallery");
}

$config['gallery_base_path']类似于 /absolute/path/to/my/application/locations .

由于不知道文件操作是否会使用包含路径,可能会导致额外的困惑,可以告诉某些文件选项使用包含路径,其中可能包含当前文件路径和当前脚本目录,但如果没有,它只会尝试从当前工作目录中查找该文件。而includerequire都使用包含路径和当前脚本目录作为查找脚本的潜在点。

P.S.P.S 使用 getcwd找出脚本中任意位置的当前工作目录。

关于javascript - php 库无法与 AJAX 配合使用的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35723710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com