簡體   English   中英

ajax成功內的ajax

[英]ajax inside an ajax success

我做了一個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;
});

一個簡單的示例“ 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處找到的腳本的返回值。 希望對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM