简单的Ajax / codeigniter请求简单、Ajax、codeigniter

2023-09-10 14:35:41 作者:公子少保

我在使用Ajax和codeigniter一些问题。我已经发布了另一个问题(链接质疑)和我想我解决了这个问题,但我没那么我真的请人编写简单的code与AJAX / codeigniter,这将增加点击里面DIV /段数。

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

我会更高兴有简单的例子来做到这一点。我也试过,同样,但没有任何运气。下面是我想这个时候:

控制器(的welcome.php)

 < PHP的,如果(定义(BASEPATH')!)出口('没有直接的脚本允许访问);

一流的欢迎扩展是CI_Controller {


功能__construct()
    {
        父:: __结构();
    }

公共功能指数()
{
    $这个 - >负载>查看('WELCOME_MESSAGE');
}

功能的增加(){
    $增加= $这个 - >输入 - >后期('升高');
    回声增长++;
}
}
 

JS(阿贾克斯)

 功能增加(){
VAR数= parseInt函数($('#号)HTML())+ 1;
$阿贾克斯({
        键入:POST,
        网址:本地主机/测试/欢迎/增长,
        数据:{增加:数},
        成功:函数(响应){
            $('#号)HTML(响应)。
        }
});

}
 

查看(HTML / CSS)

 <!DOCTYPE HTML>
< HTML LANG =EN>
< HEAD>
<脚本类型=文/ JavaScript的>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =< PHP的回声BASE_URL();?>资产/ JS / jquery_v1.9.1.js> < / SCRIPT>
<脚本类型=文/ JavaScript的SRC =< PHP的回声BASE_URL();?>资产/ JS /的script.js> < / SCRIPT>
<风格类型=文本/ CSS>
#数 {
显示:块;
文本对齐:中心;
宽度:100像素;
高度:30PX;
保证金:自动汽车;
的line-height:30PX;
边界:1px的固体#999999;
边界半径:5像素;
}

< /风格>
< /头>
<身体GT;
    <跨度ID =数字的onclick =增加()> 0℃/ SPAN>
< /身体GT;
< / HTML>
 
ajax的简单请求和特殊请求

我使用的是最新的XAMPP在Windows 7上的错误,我得到当我点击跨度 - POST的http://本地主机/测试/本地主机/测试/欢迎/加404(未找到)

解决方案

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

您可以使用此插件检索饼干在JavaScript。 并简单地将其传递给CI。

  

ci_token

  

ci_cookie

密钥可以是不同的,并且可以发现 在config.php

我也建议设立一个路由请求和使用

  

SITE_URL()

  

BASE_URL()

  VAR SITE =< PHP的回声SITE_URL();?>中//全局变量,这样你的JavaScript代码可外接
 

-

  VAR数据= {'ci_token:$ .cookies.get('ci_cookie),增加形式:parseInt(数)}
$阿贾克斯({
    网址:网站+/链接/到/控制器/法,
    数据:数据,
});
 

I'm having some issues with ajax and codeigniter. I've already posted another question (link to question) and I thought I solved it, but I did not so I`m asking someone to write simple code with ajax/codeigniter that will increase number inside div/span on click.

I`m trying last few days to do this, but constantly getting errors..My CI setting are : base_url : localhost/test/ index:index.php autoload:url default controller:welcome (I left it so just for this test)

I would be more than happy to have simple example to do this. I tried also, again, but without any luck. Here's what I tried this time :

Controller (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 (Ajax)

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);
        }
});

}

View (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>

I'm using latest xampp on windows 7. Error that I get when I click on span - POST http://localhost/test/localhost/test/welcome/increase 404 (Not Found)

解决方案

You must submit the CSRF token from cookies otherwise the request will be invalid, if you have CSRF enabled in config.php.

You can use this plugin to retrieve cookies in javascript. And simply pass it to CI.

ci_token

and

ci_cookie

keys may be different and can be found in config.php

I would also suggest setting up a route for the request and using

site_url()

over

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,
});