Codeigniter:Ajax Request,控制器中的全局变量

时间:2013-06-19 04:35:54

标签: php ajax codeigniter jquery

我在__construct();

中设置全局变量
function __construct()
{
        parent::__construct();

        //variables
        $this->galleryID = $this->uri->segment(3);
        $this->productID = $this->uri->segment(4);
}

从下拉菜单中选择后,我发出ajax请求。

$.ajax(
    {
        type: 'POST',
        url: '/beta/checkout/getCoverSizes',
        data: {
            column: size
        },
        dataType: 'json',
        success: function (json)
        {
            console.log(json);
        }
    });

此时,只需输出全局变量

public function getCoverSizes()
    {
        print_r($this->productID);
}

目前没有什么是$ this-> productID返回0,我肯定它是正确的,因为函数index()依赖于此变量并且正确地呈现数据。 ajax请求似乎没有访问全局变量$ this-> productID。

3 个答案:

答案 0 :(得分:0)

$.ajax(
    {
        type: 'GET',  // use GET here
        url: '/beta/checkout/getCoverSizes',
        data: {
            column: size
        },
        dataType: 'json',
        success: function (json)
        {
            console.log(json);
        }
    });

答案 1 :(得分:0)

$this->uri->segment(3);使用galleryID$this->uri->segment(4);使用productIDajax调用中的网址没有这些参数您应该传递这些ID在ajax调用中为了得到像

$.ajax(
{
    type: 'POST',
    url: '/beta/checkout/getCoverSizes/1/2',
    //url: '/beta/checkout/getCoverSizes/galleryID/productID',
    data: {
        column: size
    },
    dataType: 'json',
    success: function (json)
    {
        console.log(json);
    }
});

在你的课堂上,我假设你已经定义了像

这样的全局变量
class checkout extends CI_Controller {
public $galleryID;
public $productID;
// your other code
}

答案 2 :(得分:0)

在java-script中将值js传递给globle ajax

$("#abc").on("change",function(){
var post_data = new FormData();
ajax_request("dashboard/ajax",post_data, response,null);
});

然后在JS

function ajax_request(URL, request_data, response_function, element){
    $.ajax({
        type: "POST",
        datatype:"json",
        url: BASE_URL+URL,
        data: request_data,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function(result){
            response_function(JSON.parse(result), element);
        },
        error: function() {
            response_function(undefined, element);
        }
    });
}
相关问题