停止

2013-01-08 28 views
0

我使用layout方法來呈現我在CI框架模板中的控制器動作中渲染的佈局,我添加了一些修改的代碼,停止渲染的佈局,如果請求AJAX停止

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 

    return; 

    } else { 
     //render the layout (the code from example in the link) 
} 
鏈接

我面對的問題是我使用AjaxFileUpload將文件上傳到服務器,請求類型是同步的,這意味着渲染布局!響應以jSon + HTML的形式返回,這是佈局條件的正常流程,如果請求在上傳文件時同步,我應該如何防止呈現佈局。

這裏是JS

$.ajaxFileUpload({ 
url : url ", 
secureuri :false, 
fileElementId :'imageFile', 
dataType : 'json', 
type: "POST", 

success : function (data) 
{ 
    console.log(data); 

}, 
error: function (request, status, error) { 

} 
}); 

,在這裏我從服務器返回

echo json_encode(array('status' => $status, 'msg' => $msg)); 
return; 

回答

0

只爲這通一個簡單的解決方案空給你佈置可變

class Welcome extends CI_Controller { 

    public $layout = 'default'; 

    public function index() 
    { 
     $this->load->view('welcome_message'); 
    } 

    public function ajax_call() 
    { 
     $this->layout = null; 
     echo json_encode(array('status' => $status, 'msg' => $msg)); 
     // you can use this but your layout should be null 
     $this->output 
    ->set_content_type('application/json') 
    ->set_output(json_encode(array('foo' => 'bar'))); 
    } 
} 
+0

它的工作原理但它會導致此警告「資源解釋爲Document,但使用MIME類型application/json:url傳輸」 但我修復它使得js中的數據類型爲jsonp – palAlaa

+0

響應返回爲

 {"status":"error","msg":"image has uploaded successfully"}
您是否有任何建議haw可以刪除pre標籤! – palAlaa

+0

您使用的是哪種方法echo json_encode或設置輸出 – umefarooq