简体   繁体   English

来自Ajax请求的Codeigniter和数据

[英]Codeigniter and data from ajax request

I've been playing with this for a while and not able to get it to work. 我已经玩了一段时间,但无法使其正常工作。 I am using CodeIgniter and on click of button $('.galName').click(initUpdate); 我正在使用CodeIgniter并单击按钮$('.galName').click(initUpdate); I am calling function: 我正在调用函数:

function initUpdate()
{
    var id = $(this).attr('id').split('_')[1];
    //grab id from link update_xxxx

    //get gallery information
    $.ajax(
    {
        type: 'POST',
        url: 'ajax/update',
        data: id,
        dataType: 'json',
        success: function(json)
        {
            alert(json);
        }
    });
}

I can alert the id after var is set and that's correct. 设置了var后,我可以提醒ID,这是正确的。 Here is the function called within CodeIgniter controller. 这是在CodeIgniter控制器中调用的函数。

public function update()
        {
            $id = $this->input->post('id');

            echo json_encode($id);
        }

If I set $id="something" and alert(json) it works fine and alerts whatever I set something to. 如果我设置了$id="something"alert(json)它将正常工作并警告我设置的内容。 However, my issue is with $id = $this->input->post('id'); 但是,我的问题是$id = $this->input->post('id'); It's not pulling my data from the ajax request. 它不是从ajax请求中提取我的数据。 This must be the incorrect way of doing within CodeIgniter and I haven't been able to track down the solution otherwise. 这肯定是在CodeIgniter中执行的错误方法,否则我将无法找到解决方案。 Any help would be much appreciated. 任何帮助将非常感激。

Thanks! 谢谢!

You did not set the key part of the key/value pair, try 您尚未设置键/值对的键部分,请尝试

function initUpdate()
{
    var id = $(this).attr('id').split('_')[1];
    //grab id from link update_xxxx

    //get gallery information
    $.ajax(
    {
        type: 'POST',
        url: 'ajax/update',
        data: {"id": id},// set the "id" key
        dataType: 'json',
        success: function(json)
        {
            alert(json);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM