我做了一个阿贾克斯网站从/pages
文件夹中调用页面ajax-container
div在我的index.php
。第二个Ajax请求不起作用
现在我想使第一AJAX请求成功,但第二个请求将只在某些码页之后第二个Ajax请求,
例如:我叫work.php
的页面,而这个页面里面我想打电话给页work-slider
从同一个文件夹并替换由工作slider.php的work.php网页在我的Ajax的div容器,当我在图像link-in
点击,但我不能找到使它成为一个解决方案,
这是我的实际代码:
index .PHP:
<div id="ajax-container">
<?php
$d = "pages/";
if (isset($_GET['p'])) {
$p = strtolower($_GET['p']);
if (preg_match("/^[a-z0-9\-]+$/", $p) && file_exists($d . $p . ".php")) {
include $d . $p . ".php";
} else {
include $d . "404.php";
}
} else {
include $d . "home.php";
}
?>
</div>
的Ajax功能:
var afficher = function(data) {
$('#ajax-container').fadeOut(250, function() {
$('#ajax-container').empty();
$('#ajax-container').append(data);
$('#ajax-container').fadeIn(100, function() {});
});
};
var lastRequest = null;
if (lastRequest !== null) {
lastRequest.abort();
}
var loadPage = function(page, storeHistory) {
if (typeof storeHistory === 'undefined') {
storeHistory = true;
}
lastRequest = $.ajax({
url: "pages/" + page,
cache: false,
success: f$.ajax({
url: "pages/" + page,
cache: false,
success: function(resultFirst) {
afficher(resultFirst);
if (storeHistory === true) {
history.pushState({
'key': 'value',
'url': page
}, '', page);
}
$.ajax({
// How can i define pageIn variable ??
url: "pages/" + pageIn,
cache: false,
success: function(resultSecond) {
afficher(resultSecond);
}
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
afficher('erreur lors du chagement de la page');
}
});
return false;
};
window.addEventListener('load', function() {
setTimeout(function() {
window.addEventListener('popstate', function(e) {
if (e.state === null) {
loadPage('home.php');
} else {
loadPage(e['state']['url'], false);
}
});
}, 0);
});
// link inside index.php
$('.link').bind('click', function(e) {
e.preventDefault();
var page = $(this).attr('href');
loadPage(page);
return false;
});
// second link inside called page
$('.link-in').bind('click', function(e) {
e.preventDefault();
var pageIn = $(this).attr('href');
loadPage(pageIn);
return false;
});
目前尚不清楚你想要做什么,但是我感觉你在被叫网页上绑定了一些事件,甚至在你叫它之前。在获得页面后,您应该将点击绑定到链接。 –
我只想在第一个之后做一个其他的ajax请求,比如:我现在调用work.php,然后从work.php中调用work-slider.php,然后用work-slider.php替换work.php ajax-container div在我的index.php – Tiaw
你的意思是你想从你在服务器端获得的PHP页面发出ajax调用? Mmmmhhhhh –