dongzong8110 2009-11-24 12:12
浏览 30
已采纳

用ajax调用php控制器(如何禁用布局)

I will explain more, because it might not be clear enough

I want to set the ajax url to: www.domain/controller/method I already have everything in place for this with normal php.

But if I have to do this with ajax, I am not getting the intended result. The result should be some json variable being echo'd back to me. It is not entirely clear to me why.

In firebug I can see that the requestheader is not the same as the responseheader. I see the layout view in the response. I do not know yet how to bypass that. The basecontroller creates the view for the layout, but I have not extended the ajaxcontroller with the basecontroller?? For now I am running it threw another script that I call, but I would like it more if I could do it by the first method.

Has anyone some suggestions, please ?

EDIT

It seems after the comments below I need to provide some logic to disable the layout?

first attempt:

class testController extends baseController implements IController
{

    public function testit()
    {
    $this->disableLayout = TRUE;
    $check='testit';
    $data =array();
    header('Content-type: application/json');

            $output = array(
            "check" => $check,
            "user" => $data
            );

            $this->content = json_encode($output);

            exit(0); // Stop script.

    }
}

thanks, Richard

  • 写回答

4条回答 默认 最新

  • dougui4325 2009-11-24 12:34
    关注

    I'm making a guess here, but I think you are trying to serve different content from same action depending how it was requested.

    To detect weather the page was requested by Ajax you could use specific header sent by browser.

    Usually most recent JavaScript libraries send

    X-Requested-With:XMLHttpRequest
    

    header together with their ajax request. If yours doesn't you could easily make it to send it using something like this:

    //Just example using raw XMLHttpRequest
    var request = new XMLHttpRequest();     
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    //If you use Javascript library, see the documentation how to set custom request headers.
    //But as I said most modern libraries already send X-Requested-With
    

    Then in PHP you could check if request was made using Ajax by using this code block:

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
               strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
       //Send JSON.
    } else {
       //Send HTML.
    }
    

    EDIT: As Elzo Valugi mentioned, don't forget to use correct content type when outputing JSON in your action by using:

    header('Content-type: application/json');
    

    before sending any JSON or else you might have some problems with some JavaScript libraries.


    http://i47.tinypic.com/o5we91.jpg

    This is more or less how the request + response headers should look like. Note that I'm actually sending some unneeded ones here like Prototype version, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)