2016-08-04 104 views
0

我做了一個ajax網站,調用php頁面從我的index.php裏面的/pages文件夾,在我的頁面裏面painting.php我有一個鏈接,調用painting-slider.php頁面。ajax裏面ajax成功

那麼我怎麼能打開這個painting-slider.php在ajax時,我已經打電話給我的painting.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; 
}); 
+2

你想達到什麼目的?爲什麼不簡單地在第一個ajax請求的'.success()'方法內部做一個新的ajax請求呢? – messerbill

+0

是的,但我必須重新創建一個ajax容器div與我的painting.php頁面中的PHP包含功能?以及我在哪裏必須插入我的seconde ajax函數? – Tiaw

回答

2

後的「AJAX一個簡單的例子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中找到的腳本的返回值。我希望它有幫助

+1

嗯好的確實是比我想的更容易,工作很好謝謝 – Tiaw