2013-06-19 90 views
0

我在__construct()中设置全局变量;Codeigniter:Ajax请求,控制器中的全局变量

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); 
} 

目前没有什么是$这个 - > ProductID等于返回0,我肯定它是作为函数的索引正确的是()取决于在这个变量上并且正在渲染数据。 ajax请求似乎没有访问全局变量$ this-> productID。

+0

。在你的代码中没有任何全局变量 – zerkms

回答

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

您正在使用$this->uri->segment(3);galleryID$this->uri->segment(4);productIDajax调用的URL没有这些参数你应该通过在Ajax调用这些ID为了得到这样

$.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 
} 
0

java-script pass价值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); 
     } 
    }); 
}