jquery Ajax 调用 Codeigniter 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27031489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:20:03  来源:igfitidea点击:

jquery Ajax call Codeigniter Controller

jqueryajaxcodeignitercontroller

提问by venky

In my View page jquery Ajax call like this

在我的视图页面中 jquery Ajax 调用是这样的

onclick: function() {
                        $.ajax({
                            type:'POST',
                            url:"<?PHP echo base_url('trand/report/checking'); ?>",
                            data: {nm:'vnky'},
                            success: function(){
                                  alert("success");
                              },
                              error: function(){
                                alert("error");
                              }
                        });
                        chart2.exportChart({
                            type: 'image/png', 
                            filename: dynmicfilename
                        });

                    }

exportchart function works perfectly .Inside ajax call also working alerts nice, but url is not executed, by using firebug when clicking the url in new tab , then it works fine.

exportchart 功能完美运行。在 ajax 调用中也可以很好地发出警报,但是 url 没有执行,通过在新选项卡中单击 url 时使用 firebug,然后它可以正常工作。

How can I execute url in ajax call. can you help on this ?

如何在 ajax 调用中执行 url。你能帮忙吗?

回答by Minh Quy

Here's my code. My controller return jsondata

这是我的代码。我的控制器返回json数据

$('.edit').click(function() {
    $.ajax({
        url: '<?php echo site_url('your_controller'); ?>',
        type: 'POST',
        data: {
            key: value
        },
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    });
});

Inside Ajax call, which alert is shown successor error? I think your JS code is correct. You should check your controller. If you open it in browser and it work fine. You should check csrf_protectionconfig is TRUE or FALSE

在 Ajax 调用中,哪个警报显示成功错误?我认为您的 JS 代码是正确的。你应该检查你的控制器。如果你在浏览器中打开它并且它工作正常。您应该检查csrf_protection配置是 TRUE 还是 FALSE

回答by kay

function generate_dateOfBirth()
{

    $data = $this->input->post('data', TRUE);

    if (!empty($data))
    {

        // Check if the ID Number supplied is not less than 13 characters
        if ( strlen(trim($data)) == 13)
        {
            $year = substr($data, 0, 2);
            $month = substr($data, 7, 2);
            $day = substr($data, 4, 2);

            $dateOfBirth = $year .'/'. $month .'/'. $day ;

            echo $dateOfBirth;
        }
        else
        {
            echo 'You have entered Invalid ID number above';
        }

    }
} 

$('#id_number').on('change', function()
        {
            var dob = $(this).val(); 

            $.ajax({

                    url: '/generate_date',
                    method: 'POST',
                    data: 'data=' + dob,
                    cache: false,
                    type: 'json',
                    success:function(data){
                      //update Increase month
                      $('#dob').val(data);
                    }

            }); //End of ajax call

        }); $route['generate_data'] = 'Controller Name/function in the controller doesn"t contain html';