2017-07-27 157 views
0

我試圖解決這個問題很長一段時間,並沒有絲毫的線索。 我想在CodeIgniter框架內發送和捕獲AJAX請求。 與頁面tamplate PHP文件:CodeIgniter CSRF 403錯誤

<table id="jivoClients" class="display nowrap" cellspacing="0" width="100%"> 
    <tbody> 
    <?php foreach($clients as $client): ?> 
      <td class="details-control" id="<?php echo $client["chat_id"]. "," 
         .$this->security->get_csrf_token_name(). "," 
         .$this->security->get_csrf_hash(); ?>"></td> 
      <th class="clientData"><?php echo str_replace(",", "<br>" , $client["visitor_info"]); ?></th> 
      <th class="clientData"><?php echo ($client['session_geoip_country']); ?></th> 
      <th class="clientData"><?php echo ($client['session_geoip_city']); ?></th> 
      <th class="clientData"><?php echo ($client['visitor_chats_count']); ?></th> 
      <th class="clientData"><?php echo ($client['agents_names']); ?></th> 
      <th class="clientData"><?php if(isset($client['messages']['0']['timestamp'])): ?> 
       <?php echo date('m/d/Y', $client['messages']['0']['timestamp']); ?> 
       <?php endif;?></th> 
      <th class="clientData"><?php if(isset($client['messages']['0']['timestamp'])): ?> 
       <?php echo date('H:i:s', $client['messages']['0']['timestamp']); ?> 
       <?php endif;?></th> 
      <th class="clientData"><?php if(isset($client['messages']['0']['Message'])): ?> 
       <?php echo ($client['messages']['0']['Message']); ?> 
       <?php endif;?></th> 
      <th class="clientData"><?php echo ($client['Manager_note']); ?></th> 
     </tr> 
    <?php endforeach; ?> 
    </tbody> 
</table> 

在我的js文件的代碼如下所示:

var id = $(this).attr('id'); 
    var messageData = id.split(","); 
    var token = "" + messageData[1]; 
    var post_data = { 
     'id' : messageData[0], 
     'csrf_crm' : messageData[2] 
    } 
    $.ajax({ 
     type:'POST', 
     data: { 
      func : 'getNewLocations', 
      'id' : messageData[0], 
      'csrf_crm' : messageData[2] 
     }, 
     url:'application/controllers/AjaxController.php', 

     success: function(result, statut) { 
      if (result == 'add') { 
       //do something 
      } 
      else if (result == 'remove') { 
       //do something 
      } 
     } 
    }); 

而且我不斷收到403錯誤。正如我已經在論壇上閱讀的那樣,這意味着我的CSRF令牌不正確。但似乎我通常得到它(在調試器中檢查)。 但是這並不能解決問題。 在此先感謝。

+1

在配置文件集$ config ['csrf_regenerate'] = TRUE;到$ config ['csrf_regenerate'] = FALSE; –

+0

確保您的配置文件中的csrf令牌重新生成爲false,否則它將在每個請求中重新生成一個新令牌。 $ config ['csrf_regenerate'] = FALSE; –

+0

做到了。沒有結果 –

回答

0

最後我解決了這個問題。 它顯示,我在js文件中使用不正確的路線。 的動作的正確序列看起來像這樣: 1)在文件routes.php文件添加新行:

$route['jivosite/user'] = 'jivosite/user'; 

2)在js文件添加代碼: post_data = JSON.stringify(post_data);

post_data = JSON.stringify(post_data); 
    var url = baseurl + "jivosite/user" 
    var xhr = new XMLHttpRequest(); 
    xhr.open("POST", url); 
    xhr.setRequestHeader("Content-Type", "application/json"); 
    xhr.addEventListener("readystatechange", function() { 
     if (xhr.readyState == 4 && xhr.status === 200) { 
      console.log(xhr.response); 
     } 
    }); 
    xhr.addEventListener("error", function (err) { 
     console.log(err); 
    }); 
    xhr.send(post_data ? JSON.stringify(post_data) : null); 

3)檢查config.php文件。 $config['cookie_secure'] = FALSE;應該是這樣的 和$config['csrf_exclude_uris'] = array('jivosite/user');應包括我的路線

4)控制器文件夾中創建文件Jivosite.php與內容:

class Jivosite extends CI_Controller 
{ 
    public function user() 
    { 
     $id=$this->input->post('id'); 
     echo $id; 
    } 
} 

而且它爲我工作。 感謝您的所有答案。

2

您需要做echo來指定php變量的值。此外包裹令牌''

var token = '<?php echo $this->security->get_csrf_hash() ?>'; //<----- do echo here 
    var id = $(this).attr('id'); 
    $.ajax({ 
       type:'POST', 
       data: { 
        func : 'getNewLocations', 
        'id' : id, 
        'csrf_crm' : token 
       }, 
       url:'application/controllers/AjaxController.php', 

       success: function(result, statut) { 
        if (result == 'add') { 
         //do something 
        } 
        else if (result == 'remove') { 
         //do something 
        } 
       } 
      }); 
+0

感謝您的糾正,但它並沒有解決主要問題 - toen不被CodeIgniter接受 –

+0

將'csrf_crm'改爲'<?php echo $ this-> security-> get_csrf_token_name(),?>'然後嘗試。我也懷疑'func:' –

+0

不能將php代碼插入到js文件中。所以我從配置文件複製了名字並將其插入到數據數組中。至於功能,現在不要太在意它,因爲它不能正常工作。 –

0

獲取CSRF令牌值是這樣的:

var token = $('[name="csrf_token"]').val(); 
+0

謝謝。 Interresting獲取令牌名稱的方式。但在我看來,這不是問題,因爲令牌發送不正確。 –

+0

還有兩件事你可以申請並交叉檢查1.網址(也可以嘗試附加baseurl),2。將'csrf_crm'更改爲'csrf_token' –

+0

1)將'csrf_crm'更改爲'csrf_token' - 無結果 –