ci-ajax-csrf-problemの解決策を確認した後、スクリプトに次の行を追加しましたが、正常に機能します。
var post_data = {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}
に挿入
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
data: post_data,
みんな助けてくれてありがとう:)
私はAjax/Jqueryを初めて使用し、jorge torresのCodeIgniter用のAjaxのガイドに従って、自分のWebサイトに簡単なajax呼び出しを実装し、問題が発生しました。
Controller Ajaxを作成しました。これは、コードスニペットです。
class Ajax extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
$output_string = 'This is a test';
echo json_encode($output_string);
}
public function test2(){
$this->load->view('test.php');
}
}
これはそのコントローラーのビューであり、URLヘルパー$ this-> load-> helper('url');を追加してロードしたことを除いて、チュートリアルのビューと同じです。最初の行
スクリプトコードのスニペットは次のとおりです。
#getdataはボタンタイプで、#result_tableはdivです
$('#getdata').click(function(){
$.ajax({
url: '<?php echo base_url().'ajax/test';?>',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
localhost.com/codeigniter/ajax/test2に正常にアクセスできますが、ボタンをクリックしても何も起こりません。
ページのソース情報を見てみましたが、URLは正しいです
$.ajax({
url: 'http://localhost/codeigniter/ajax/test',
type:'POST'
....
localhost / codeigniter / ajax / testに直接アクセスすることも可能で、出力メッセージが表示されます。
CodeIgniter 2.1.3を使用していて、ローカルホストはXAMPP1.7.3で実行されています
前もって感謝します :)