2017-08-01 71 views
1

Windows 10,Codeigniter 3,Wamp3。'Bad Request'with ajax and codeigniter

Ajax post引發錯誤的請求錯誤。這是一個古老的栗子,但在線研究表明這個問題通常是CSRF。但是我要強調的是我已經CSRF禁用此測試開始:

config['csrf_protection'] = FALSE; 

我已經設置了一些故意非常簡單的測試代碼。控制器看起來是這樣的:

class Ajax extends CI_Controller { 

public function index() { 


$this->load->view('pages/index'); 
} 

public function hello($name) { 
    $fullname = $this->input->post('fullname'); 
    echo 'Hello '.$fullname; 

} 

}//EOF 

和觀點如下:

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Demo Ajax</title> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script> 
    $(function() { 
     $('#bttHello').click(function(){ 
      var fullname = $('#fullname').val(); 
      $.ajax({ 
       type:'POST', 
       data: {fullname: fullname}, 
       url:'<?php echo base_url('ajax/hello'); ?> + fullname', 
       success: function(result) { 
        $('#result1').html(result); 

       } 
      }); 

     }); 

    }); 
</script> 
</head> 
<body> 

Name <input type="text" id="fullname"> 
<input type="button" value="Hello" id="bttHello"> 
<br> 
<span id="result1"></span> 

</body> 
</html> 

控制檯顯示一個錯誤的請求

POST XHR http://localhost/faith/ajax/hello%20+%20fullname [HTTP/1.1 400 Bad Request 9ms] 

所以,如果CSRF是不是罪魁禍首,是它一個Wamp的問題?其他一切似乎都很好。我花了這麼多時間在這個上! 這是怎麼回事? /fullname

+0

你應該檢查你的服務器錯誤日誌中實際的錯誤,而不是試圖猜測問題。 –

+0

首先你使用ajax後,然後你爲什麼發送數據params附加URL。不需要發佈數據就足以向控制器方法發送參數。其次,你好帕拉姆有名稱參數需要然後你期望通過正常的職位。第三,你從ajax中得到的參數是好的,那麼在這些美麗的括號內,參數的名字是什麼。如果它的可選參數使它們可選。從ajax url中刪除參數,並在method中設置方法參數。你很好去.. –

回答

1

數據通過POST已經發送。無需通過它通過URL

<script> 
$(function() { 
    $('#bttHello').click(function(){ 
    var fullname = $('#fullname').val(); 
    $.ajax({ 
     type:'POST', 
     data: {fullname: fullname}, 
     url:"<?php echo base_url('ajax/hello'); ?>", 
     success: function(result) { 
     $('#result1').html(result); 
     } 
    }); 
    }); 
}); 
</script> 

而且,從控制器動作hello()刪除參數$name

public function hello() { 
    $fullname = $this->input->post('fullname'); 
    echo 'Hello '.$fullname; 
} 
+0

這工作完美 - 非常感謝!現在我有另一個問題,因爲當我啓用csrf我想從cookie中獲取值,但jquery.cookie.js不適合我... – Perkin5

+0

@ Perkin5:我希望我能幫助你解決新問題。但是,我以前沒有在codeigniter中工作過。所以,我不會把你從路上轉移。我會建議你提出關於CSRF問題的另一個問題。 *請不要介意。* –

-2
use this way 
you should concate fullname variable after quatation. 
like this 
url:'<?php echo base_url('ajax/hello'); ?>' + fullname 


<script> 
$(function() { 
    $('#bttHello').click(function(){ 
     var fullname = $('#fullname').val(); 
     $.ajax({ 
      type:'POST', 
      data: {fullname: fullname}, 
      url:'<?php echo base_url('ajax/hello'); ?>' + fullname, 
      success: function(result) { 
       $('#result1').html(result); 

      } 
     }); 

    }); 

}); 

+2

「* ..使用這種方式*」。你能否解釋爲什麼OP會使用這個答案。每個答案都需要解釋。 –

0

寫網址這樣

"url": "<?php echo base_url().'ajax/hello';?>/" + fullname 

其一個function hello()argument

0

試試這個..

<?php echo form_open('ajax/hello', [ 
      'method' => 'post', 
      'class' => 'create_form' 
     ]); ?> 
     <input type="text" name="fullname" value="Full Name"/> 
<button type="submit">Create</button> 
    <?php echo form_close(); ?> 

和Ajax

$(document).on('submit', 'form.create_form', function (e) { 
      var self = this; 
      var formData = new FormData($(this)[0]); 
      $.ajax({ 
       url: $(self).attr('action'), 
       type: 'POST', 
       data: formData, 
       async: false, 
       dataType: 'json', 
       success: function (res) { 
        console.log(res) 
       }, 
       cache: false, 
       contentType: false, 
       processData: false 
      }); 
      return false; 
     }); 
0

的笨控制器:

<?php 
class Ajax extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->view('pages/index'); 
    } 
    /** 
    * @param $name 
    */ 
    public function hello($name) 
    { 
     // if no $name params value pass and post exist 
     if (! isset($name) && $this->input->post('fullname')) { 
      // get value from post params 
      $fullname = $this->input->post('fullname', true); 
     } elseif (isset($name) && ! $this->input->post('fullname')) { 
      // get value from pass param method 
      $fullname = $name; 
     } else { 
      // default value 
      $fullname = 'No Name found'; 
     } 
     // show ajax response 
     echo $fullname; 
    } 
    /** 
    * Another way if we using GET params 
    * e.g. http://wwww.site.com/ajax/hello/my-name-value 
    * @param $name 
    */ 
    public function hello_another($name) 
    { 
     // if url has param as name value 
     // remember codeigniter will map all url params as method params as they provided 
     // no need to use get input, it will take from url string directly 
     if (isset($name)) { 
      // get value from get params 
      $fullname = $name; 
     } else { 
      // default value 
      $fullname = 'No Name found'; 
     } 
     // show ajax response 
     echo $fullname; 
    } 
    /** 
    * Another way if we using GET params and security is on top 
    * e.g. http://wwww.site.com/ajax/hello/my-name-value 
    * @param $name 
    */ 
    public function hello_another_secure($name) 
    { 
     // if url has param as name value 
     // remember codeigniter will map all url params as method params as they provided 
     // no need to use get input, it will take from url string directly 
     if (isset($name) && preg_match("/^[a-zA-Z'-]+$/", $name)) { 
      // get value from method params 
      $fullname = $name; 
     } else { 
      // default value 
      $fullname = 'No Name or invalid name found'; 
     } 
     // show ajax response 
     echo $fullname; 
    } 
} 
//EOF 

<script> 
    $(function() { 
     $('#bttHello').click(function(){ 
      var fullname = $('#fullname').val(); 
      $.ajax({ 
       type:'POST', 
       data: {fullname: fullname}, 
       url:'<?php echo base_url('ajax/hello'); ?>', 
       success: function(result) { 
        $('#result1').html(result); 

       } 
      }); 

     }); 

    }); 
</script> 

<script> 
    $(function() { 
     $('#bttHello').click(function(){ 
      var fullname = $('#fullname').val(); 
      $.ajax({ 
       type:'GET', 
       url:'<?php echo base_url('ajax/hello_another/'); ?> + fullname', 
       success: function(result) { 
        $('#result1').html(result); 

       } 
      }); 

     }); 

    }); 
</script> 

的笨完全能夠滿足您的需求,只要看看他們的AWESOME Document第一..