doufei0933 2016-12-31 00:24
浏览 46
已采纳

应该jquery ajax转到同一页面或另一个PHP文件?

My Current Setup

  1. my sites home page (rankings.php) shows a list of users with a search form at the top to filter users by their gender or city.

  2. I want to use jquery ajax to update the results without reloading the page.

  3. My Question:

    • with either method (load,post,get) you need to enter the url to send the request to but I'm not sure if the url should be the rankings.php page itself or another php file that holds the mysql query to get new results from the database. Which is best? should ajax requests go to another php and then the results sent back to the main php file where the content will be reloaded or should the ajax request go to the same php file it came from and then jquery to replace the content with the returned html data?

shorter version of question: should ajax url point to the page the ajax request comes from or point to another php file and then the data returned back?

  • 写回答

1条回答 默认 最新

  • duanduoding2238 2016-12-31 00:30
    关注

    It should point to the URL of the script that performs whatever function you need the AJAX request to do. It can be the same or a different URL from the page that contains the caller. If it's the same page, you'll need to put conditionals in the script that detects whether it's being used to process an AJAX request or to display the regular page, usually by checking the parameters.

    For instance, you might send the AJAX request with:

    $.ajax({
        url: '<?php echo $_SERVER['PHP_SELF']; ?>',
        type: 'post',
        data: { action: 'dosomething', ... },
        ...
    });
    

    Then in the PHP script, you would do:

    if (isset($_POST['action']) {
        // code to process AJAX request
    } else {
        // code to display the page HTML
    }
    

    I generally find it simpler to put the AJAX code in a separate script, so I don't have to do that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器