Simple Ajax/Codeigniter request(简单的 Ajax/Codeigniter 请求)
问题描述
我在使用 ajax 和 codeigniter 时遇到了一些问题.我已经发布了另一个问题(问题链接),我想我解决了它,但我没有,所以我要求某人使用 ajax/codeigniter 编写简单的代码,这将在点击时增加 div/span 内的数字.
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.
我最近几天尝试这样做,但不断出错.我的 CI 设置是:
base_url : localhost/test/
索引:index.php
自动加载:网址
默认控制器:欢迎(我只是为了这个测试而离开它)
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 :
控制器(welcome.php)
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);
}
});
}
视图 (HTML/CSS)
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>
我在 Windows 7 上使用最新的 xampp.单击 span 时出现错误 - POST http://localhost/test/localhost/test/welcome/increase 404 (Not Found)
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)
推荐答案
如果您在 config.php 中启用了 CSRF,您必须从 cookie 提交 CSRF 令牌,否则请求将无效.
You must submit the CSRF token from cookies otherwise the request will be invalid, if you have CSRF enabled in config.php.
您可以使用这个插件在javascript中检索cookies.只需将其传递给 CI.
You can use this plugin to retrieve cookies in javascript. And simply pass it to CI.
ci_token
和
ci_cookie
键可能不同,可以找到在config.php中
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()
结束
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,
});
这篇关于简单的 Ajax/Codeigniter 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:简单的 Ajax/Codeigniter 请求
基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01