【问题标题】:controller not sending data codeignitter控制器不发送数据代码点火器
【发布时间】:2019-09-18 13:31:15
【问题描述】:

我有一个现成的 ci 应用程序,我正在尝试使用 ajax,但即使我尝试了许多正确的代码,get 也可以工作,但帖子从来没有我认为配置有问题 ajax_test 视图

<script src="https://code.jquery.com/jquery-3.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
   var base_url = "<?=base_url()?>";
   $(document).ready (function(){
    $('#get_bt').click(function(){
        $.post(base_url+'ajax_test/info_page' , {name:'bashir' , id:'1'}, function(data){
            alert(data);

        })
        });
});

<button id="get_bt">get</button>

控制器

<?php
 class Ajax_test extends CI_Controller {
 public function index(){
    $this->load->helper('url');
    $this->load->view("ajax_test");

}
public function info_page(){
    echo $this->input->post('name');
}
}   
?>

请注意,它适用于 get 而不是 post jquery 错误

加载资源失败:服务器响应状态为 403 (Forbidden)

【问题讨论】:

  • 检查你的网址,因为它在我这边工作正常。

标签: php jquery codeigniter


【解决方案1】:

403 错误是由配置设置引起的。在文件 /application/config/config.php 你有以下设置。

$config['csrf_protection'] = TRUE;

这是一件好事,但也需要您将 CSRF 令牌名称/值连同“名称”和“ID”一起发送到控制器。

查看Cross-site request forgery (CSRF) 上的文档,了解如何获取这些值。

或者您可以使用 GET 作为建议的另一个答案。 (GET 不检查 CSRF。)

或者您可以使用$config['csrf_protection'] = FALSE;(通常是个坏主意。)

或者您可以使用 /application/config/config.php 中的以下内容将 URI 列入白名单

$config['csrf_exclude_uris'] = array('ajax_test/info_page');

但是发送 CSRF 凭据是最安全的,因此也是最好的解决方案。

【讨论】:

    【解决方案2】:

    尝试使用

    $name = $_GET['name'];
    
    echo $name
    

    而不是使用输入->发布

    或将您的 ajax 更改为

    $.ajax({
            url: your url,
            type: "POST",
            data: {name:"bashir"},
            dataType: "json",
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                alert("success")
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert("Error");
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多