douxue7196 2011-05-25 15:59
浏览 158

在页面加载后发出两个ajax请求但只发生一次

I have two ajax requests that i need to make that will return two elements of data and i want to only grab them once as the page loads

I was thinking of this approach but i was wondering is there a better

    $(document).ready(function(){
        $.get("/shop_possystems/index.php?route=module/cart/get_subtotal",
           function(data) {
         });

        $.get("/shop_possystems/index.php?route=module/cart/get_tax",
           function(data) {
         });
});

the problem is how do i get this data to my php page so another thing i was thinking was along these lines

        <script type="text/javascript">
            $.get("/shop_possystems/index.php?route=module/cart/get_subtotal",
               function(data) {
                <?php $subtotal = data; ?>
             });

            $.get("/shop_possystems/index.php?route=module/cart/get_tax",
               function(data) {
                    <?php $tax = data; ?>
             });
        </script>

as you can see i am trying to add a script tag in the php page and trying to set the values here....both ways seem a bit hacky, not to mention the second approach is probably not even correct syntax....any suggestions

  • 写回答

2条回答 默认 最新

  • douhu2898 2011-05-25 16:11
    关注

    how do i get this data to my php page so another thing

    Your request to index.php returns the data. So index.php already has the data in the page. Simply have that page deal with the data how it needs to.

    The only other way to send data from js to PHP is a $.post request.

    There's nothing wrong with doing this to fire two get requests.

    $(function() {
      $.get(..);
      $.get(..);
    });
    

    You can also just get the PHP to get that information when you do your initial page load request and just inject the data directly into the HTML.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能