weixin_33734785 2012-04-14 19:56 采纳率: 0%
浏览 36

php网站和Jquery $ .ajax()

The website I'm developing is structured in this way:

It has a function that switches the module for the homepage content when $_GET['module'] is set, example:

function switchmodules()
{
    $module=$_GET['module'];
    switch($module)
    {
        case 'getnews': news(); break;
        default: def();
    }
}

As you can see, this switch calls another function for each module.

For example I wrote getNews() to work in this way:

function getNews()
{
   $id=$_GET['id'];
   if(!id)
   {
       //CODE TO LIST ALL NEWS
   }
   if(isset($id))
   {
       //CODE TO GET ONLY 1 NEWS BY ID
   }

}

So, as you can see I'm not using an unique file for each module; all operations of a module are part of an unique function in which an action is switched again to change the result.

If I want to get a news from database I should use an url like this: ?index.php&module=getnews&id=1

My question now is:
With jQuery and $.ajax() method is there a way to get a news (for example) using this structure based on functions switched by a get? Or do I have to change everything and make a file for each function?

  • 写回答

3条回答 默认 最新

  • weixin_33720956 2012-04-14 20:04
    关注

    you can simply call the same url via $.ajax(). the only thing which should be changed for axjax calls is that you don't output your layout, but only the news itsself.

    in php you can check for an ajax request like

    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    
        // dont output layout
    }
    
    评论

报告相同问题?

悬赏问题

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