ajax成功内的ajax [英] ajax inside an ajax success

查看:83
本文介绍了ajax成功内的ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i made an ajax website that call php pages from a /pages folder inside my index.php, inside my page painting.php i have a link that call painting-slider.php page.

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

so how can i open this painting-slider.php in ajax when i already called my painting.php page?

这是我的index.php请求页面:

this is my index.php request page:

<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函数:

and this is my ajax function:

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之后的ajax"的简单示例:

a simple example of "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处找到的脚本的返回值.希望对您有帮助

On the serverside (your PHP files) it is not important to do anything. Your ajax will just get the return value of the script found at the given Urls. I hope it helps

这篇关于ajax成功内的ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆