Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 简单Ajax/Codeigniter请求_Javascript_Ajax_Codeigniter_Onclick - Fatal编程技术网

Javascript 简单Ajax/Codeigniter请求

Javascript 简单Ajax/Codeigniter请求,javascript,ajax,codeigniter,onclick,Javascript,Ajax,Codeigniter,Onclick,我对ajax和codeigniter有一些问题。我已经发布了另一个问题(),我想我已经解决了,但我没有解决,所以我要求某人使用ajax/codeigniter编写简单的代码,这将在单击时增加div/span中的数字 我最近几天一直在尝试这样做,但不断出现错误。我的CI设置是:base\u url:localhost/test/ index:index.php 自动加载:url 默认控制器:welcome(我留下它只是为了这个测试) 我将非常高兴有一个简单的例子来做这件事。我也试了一次,但没有成功

我对ajax和codeigniter有一些问题。我已经发布了另一个问题(),我想我已经解决了,但我没有解决,所以我要求某人使用ajax/codeigniter编写简单的代码,这将在单击时增加div/span中的数字

我最近几天一直在尝试这样做,但不断出现错误。我的CI设置是:
base\u url:
localhost/test/

index:index.php
自动加载:url
默认控制器:welcome(我留下它只是为了这个测试)

我将非常高兴有一个简单的例子来做这件事。我也试了一次,但没有成功。这是我这次尝试的:

控制器(welcome.php)

视图(HTML/CSS)


使用codeigniter的
site\u 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);
     }
  });

但如果您在CI中调用控制器,则最好使用
site\u url()
,这样在您将控制器上载到live server时不会出现错误。

在ajax中,不要为JS使用外部链接,只需使用内部链接即可

确保设置$config['base\u 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);
        }
   });

}
函数增加(){
var number=parseInt($('#number').html())+1;
$.ajax({
键入:“POST”,
url:“欢迎/增加”,
数据:{增加:数量},
成功:功能(响应){
$('#number').html(回复);
}
});
}


如果在config.php中启用了CSRF,则必须从Cookie提交CSRF令牌,否则请求将无效

您可以使用它来检索javascript中的Cookie。 只需将其传递给CI即可

ci_代币

饼干

钥匙可能不同,可以找到 在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);
        }
   });

}
我还建议为请求设置一条路由并使用

网址(url)

结束

基本url()


首先,您应该在application/config文件中定义您的站点基本url,然后将此基本url与调用所在的ajax页面调用一起使用。 假设您的
基本url
http://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(){
        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);
            }
        });

 }

如果在.php文件中使用此ajax,则应该对url执行以下操作:

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);
    }
  });
}
希望它能解决这个问题。但是,在本地环境中开发时,最好在config.php中设置base_url,如下所示:

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

是站点url还是基本url?我认为这是一个基本的url。。我得到的错误:POST 403(禁止)
base\u url()
返回与site\u url相同的内容,没有附加索引页面或url\u后缀。现在我得到这个错误(如果我放置完整路径):POST 404(未找到)在声明函数
public function increme()时忘记了public{
您现在遇到了错误..因为您在ajax中拼写了函数名worn…
increase
increase
。:):)不要使用“fix”标记,除非您谈论的是fix协议。不,我不必做任何事情。我所要做的就是在ajax url中证明完整路径。我相信它与xampp有关。
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,
});
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++;
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;
    }
}
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);
            }
        });

 }
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);
    }
  });
}
<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);
    }
  });
}
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = $root;