简单的Ajax /笨请求(Simple Ajax/Codeigniter request)

2019-09-01 23:34发布

我在与AJAX和笨一些问题。 我已经发布了另一个问题( 链接问题 ),我想我解决了这个问题,但我没那么我真的请人写与AJAX /笨,这将增加数DIV / SPAN内的点击简单的代码。

我真的想最近几天要做到这一点,但经常收到errors..My CI设置有:
BASE_URL: localhost/test/
指数:的index.php
自动加载:网址
默认的控制器:欢迎(我把它这样只是为了本次测试)

我会更乐意有简单的例子来做到这一点。 我也试过,又一次,却没有任何的运气。 下面是我想这个时候:

控制器(的welcome.php)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {


function __construct()
    {
        parent::__construct();
    }

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

function increase(){
    $increase = $this->input->post('increase');
    echo increase++;
}
}

JS(阿贾克斯)

function increase(){
var number = parseInt($('#number').html()) + 1;
$.ajax({
        type: 'POST',
        url: 'localhost/test/welcome/increase',
        data: { increase:number },
        success:function(response){
            $('#number').html(response);
        }
});

}

浏览(HTML / CSS)

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery_v1.9.1.js"> </script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/script.js">            </script>
<style type="text/css">
#number {
display: block;
text-align: center;
width: 100px;
height: 30px;
margin: auto auto;
line-height: 30px;
border: 1px solid #999999;
border-radius: 5px;
}

</style>
</head>
<body>
    <span id="number" onclick="increase()">0</span>
</body>
</html>

我在Windows上使用最新的XAMPP 7.错误,我得到的,当我点击跨度- POST http://localhost/test/localhost/test/welcome/increase 404 (Not Found)

Answer 1:

你必须提交从饼干CSRF令牌,否则请求将是无效的,如果你有在config.php启用CSRF。

您可以使用此插件在JavaScript中检索cookie。 而简单地将它传递给CI。

ci_token

ci_cookie

密钥可以是不同的,可以在config.php中找到

我也建议设置为请求的路径和使用

SITE_URL()

过度

BASE_URL()

var SITE = "<?php echo site_url();?>" // GLOBAL variable so your javascripts can be external

-

var data = { 'ci_token' : $.cookies.get('ci_cookie'), 'increase' : parseInt(number)}
$.ajax({
    url : SITE + "/link/to/controller/method",
    data : data,
});


Answer 2:

使用site_url()笨的

 function increase(){
   var number = parseInt($('#number').html()) + 1;  
   $.ajax({
     type: 'POST',
     url: '<?php echo site_url("welcome/increase")?>',
     data: { increse:number }, //<--- here should be increase
     success:function(response){
        $('#number').html(response);
     }
  });

}

然而,增加http://在本地主机的前面应该工作

 url: 'http://localhost/test/welcome/increase',

但它始终是更好,推荐使用site_url()如果要调用在CI控制器......所以,虽然当你把它上传到服务器住这不会给你的错误。



Answer 3:

在你的AJAX,不使用外部链接,你的JS只需使用内部。

请确保您设置$配置[ 'BASE_URL'] = http://localhost/test/在config.php

function increase(){
    var number = parseInt($('#number').html()) + 1;
    $.ajax({
        type: 'POST',
        url: '<?php echo base_url()?>welcome/increase',
        data: { increase:number },
        success:function(response){
           $('#number').html(response);
        }
   });

}


Answer 4:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery_v1.9.1.js"> </script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/script.js">            </script>
<style type="text/css">
  #number {
    display: block;
    text-align: center;
    width: 100px;
    height: 30px;
    margin: auto auto;
    line-height: 30px;
    border: 1px solid #999999;
    border-radius: 5px;
  }

</style>
</head>
<script>
function increase(){
  var number = parseInt($('#number').html()) + 1;
  $.ajax({
      type: 'POST',
      url: '<?php echo base_url()?>welcome/increase',
      data: { increase:number },
      success:function(response){
         $('#number').html(response);
      }
  });

}
</script>
<body>
<span id="number" onclick="increase()">0</span>
</body>
</html>


Answer 5:

首先,你应该定义应用程序/配置文件的网站BASE_URL然后用这个BASE_URL与您的电话将转入您的AJAX页面调用。 假设你base_urlhttp://localhost/test/

function increase(){
   var number = parseInt($('#number').html()) + 1;
   $.post('<?php echo base_url()?>welcome/increase',{number :number},function(response){
          $('#number').html(response);
   });    
}

然后改变你增加功能控制器这样的

function increase(){
   $increase = $_POST['number'];
echo increase++;

}



Answer 6:

试试这个 :

function increase(){
        var number = parseInt($('#number').html()) + 1;
        $.ajax({
            type: 'POST',
            url: 'localhost/test/Welcome/increase/',
            data: "increase=" + number,
            dataType: "text",
            success:function(response){
                $('#number').html(response);
            }
        });

 }


Answer 7:

如果你是在一个PHP文件使用这个AJAX不是你应该做这样的事情您的网址:

function increase(){
var number = parseInt($('#number').html()) + 1;
var base_url = "<?php echo base_url();?>";
$.ajax({
    type: 'POST',
    url: base_url+'welcome/increase',
    data: { increase:number },
    success:function(response){
        $('#number').html(response);
    }
  });
}

或者如果你是在.js文件中这样做比你需要添加你的脑袋标签内这条线

<script> var base_url = "<?php echo base_url();?>";</script>

而不是使用这样的:

function increase(){
var number = parseInt($('#number').html()) + 1;
$.ajax({
    type: 'POST',
    url: base_url+'welcome/increase',
    data: { increase:number },
    success:function(response){
        $('#number').html(response);
    }
  });
}

希望能解决问题。 然而,它始终在本地环境中进行开发时,一个好主意,设置这样的config.php文件中的BASE_URL:

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = $root;


Answer 8:

View::::::    
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="<?php echo base_url();?>css/jquery.js"> </script>
<style type="text/css">
#number {
display: block;
text-align: center;
width: 100px;
height: 30px;
margin: 50px auto;
line-height: 30px;
border: 1px solid #999;
border-radius: 5px;
}
body{
cursor: default;
}
</style>
</head>
<script>
function increase(){
var number = parseInt($('#number').html());
$.ajax({
  type: 'POST',
  url: '<?php echo base_url()?>main/samp_data',
  data: { increase:number },
  success:function(response){
     $('#number').html(response);
  }
  });
 }
</script>
<body>
<span id="number" onclick="increase()">0</span>
</body>
</html>




Controller::::::
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

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

    public function samp_data(){
        $increase = $this->input->post('increase');
        echo ++$increase;
    }
}


文章来源: Simple Ajax/Codeigniter request