drfls28608 2012-07-02 19:53
浏览 34
已采纳

将8个Ajax调用变为1

So i have a picture gallery with eight categories. Each category has 400+ images, these subcategories are called there by AJAX. I also have a pagination system in the subcategories. Currently i have 8 ajax calls, which is useless, so i'd like to shrink them into one by giving 2 arguements: 'lap' which tells it which page it's on, if lap == 0 or null it gives the normal page, else it gives oldal+?page= + lap. 'Oldal' is the second arguement, it is used at the first category selection where it tells the browser which php file to open(kepek_kat1.php or kepek_kat2 ... kepek_kat8.php). 'Oldal' works great with opening the categories, going into the subcategories, but the pagination doesn't work. Here is my code of the 8 caregories:

  <div id="kepek">
    <div class="kepkat"><a href="#amator" onclick="mutikat(null,'../php/kepek_kat1.php');"><img src="../img/kepkat/amator.jpg" height="130px" width="90px" alt="Amatőrök"/><br/>Amatőr</a></div></div> <!-- only showing the first selection -->

So the first arguement is null, which gives it the page without pagination.

Here is my subcategory code:

echo "<div class='lapozo'><a onclick='mutikat($page, '../php/kepek_kat1.php');'  href='../html/blog.php#amator?page=$page'>$page</a></div>"; }

The $page is in a loop, it lists which page it is currently on, it displays the page controls.

Here is my ajax call:

function mutikat(lap, oldal)
{
            //create XMLHttpRequest object
            xmlHttpRequest = (window.XMLHttpRequest) ? 
            new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

            //If the browser doesn't support Ajax, exit now
            if (xmlHttpRequest == null)
            return;

            if(lap == 0 || lap === 0 || lap == null )
            {
            //Initiate the XMLHttpRequest object
            xmlHttpRequest.open("GET", oldal, true);
            }
            else
            {
                xmlHttpRequest.open("GET", oldal + '?page=' +lap, true);
            }
            //Setup the callback function
            xmlHttpRequest.onreadystatechange = StateChange;

            //Send the Ajax request to the server with the GET data
            xmlHttpRequest.send(null);
}
function StateChange()
{
            if(xmlHttpRequest.readyState == 4)
            {
            document.getElementById('kepek').innerHTML = xmlHttpRequest.responseText;
            }
}

there are 8 ajax calls. Please help me!

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dongye1942 2012-07-02 20:08
    关注

    I know you can load 8 ajax calls asynchronously using jQuery's $.when.then(); functions...

    Here is an example:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    
    
        $.when(
            $.getScript("http://domain.com/script_one.js"),
            $.getScript("http://domain.com/script_two.js"),
            $.getScript("http://domain.com/script_three.js"),
            $.getScript("http://domain.com/script_four.js")
        ).then(
    
            $(function() {
    
                //work with downloaded scripts and their variables here
    
            })
    
        );
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误