gpt4 book ai didi

Codeigniter 中的 AJAX

转载 作者:行者123 更新时间:2023-12-01 13:07:40 25 4
gpt4 key购买 nike

我正在编写我的第一个 Codeigniter 网络应用程序,我想使用 AJAX 为模式框提取一些信息。有人可以指导我通过一种简单的方法将 ajax 与 CI 结合起来吗?

具体来说,用户将点击一个链接,而不是被带到另一个页面,该页面将被加载到一个模式框中。

谢谢!

最佳答案

回答你的问题,因为 CI 似乎还没有实现对 javascript 库的适当支持,你可以使用这样的函数来确保你正在处理的信息来自 ajax:

http://snipplr.com/view/1060/check-for-ajax-request/

请注意,这仅适用于 jQuery 或 MoonTools 等 javascript 库

之后,你只需要像处理普通页面一样处理信息,通过 $this->input->post('field') 或 $this->input->get('字段') 清理变量。

在一个示例中(使用 jquery),假设您有一个 anchor 标记,您想要删除项目列表中的一行:

$(function() {

$('a.delete').click(function(e) {
// prevents the default behaviour of the anchor
e.preventDefault();
// gets the id stored in the anchor as attribute
var cid= $(this).attr('cid');

// instantiate and executes the ajax
$.ajax({
type: 'POST',
url: 'http://www.yoursite.com/ajax.php',
data: "action=delete&cid="+cid,
async: true,
success: function(data){
// alerts the response, or whatever you need
alert(data);
}
});
});

之后,您只需在 CI 中将代码构建为普通页面即可。请注意,我在 ajax 请求中使用了正常的 url(基本 url,参数数据),并且不知道它是否与“url_rewrited”url 一起正常工作,但如果你只是使用它们可能没有问题“url:"用于插入完整 url 的 ajax 参数。

关于Codeigniter 中的 AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1871952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com