阿贾克斯成功的阿贾克斯

时间:2016-08-04 13:26:31

标签: javascript php jquery ajax

我创建了一个ajax网站,从我的index.php中的/pages文件夹中调用php页面,在我的页面painting.php内,我有一个调用painting-slider.php页面的链接。

那么当我已经调用了我的painting.php页面时,如何在ajax中打开此painting-slider.php

这是我的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, page) {

    $('#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: function(html) {
            afficher(html, page);
            if (storeHistory === true) {
                history.pushState({
                    'key': 'value',
                    'url': page
                }, '', page);
            }
        },
        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').bind('click', function(e) {
    e.preventDefault();
    var page = $(this).attr('href');
    loadPage(page);
    return false;
});

1 个答案:

答案 0 :(得分:2)

“ajax after ajax”的一个简单例子:

$.ajax({
    url: "pages/" + page,
    cache: false,
    success: function(html) {
        afficher(html, page);
        if (storeHistory === true) {
            history.pushState({
                'key': 'value',
                'url': page
            }, '', page);
        }
        $.ajax({
          url: otherUrl,
          cache: false,
          success: function(result) {
            alert("i am the second result");
            alert(result);
          }
      });
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        afficher('erreur lors du chagement de la page');
    }
});

在服务器端(您的PHP文件)上做任何事都不重要。您的ajax将只获取在给定Urls中找到的脚本的返回值。我希望它有所帮助

相关问题