[英]Codeigniter: Ajax Request, global variable in controller
I setting the global variable in the __construct(); 我在__construct()中设置了全局变量;
function __construct()
{
parent::__construct();
//variables
$this->galleryID = $this->uri->segment(3);
$this->productID = $this->uri->segment(4);
}
After a selection is made from a dropdown menu I make an ajax request. 从下拉菜单中选择之后,我提出了一个ajax请求。
$.ajax(
{
type: 'POST',
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
And at this point, simply output the global variable 至此,只需输出全局变量
public function getCoverSizes()
{
print_r($this->productID);
}
Currently nothing is $this->productID is returning 0, and I am positive that it is correct as function index() depends on this variable and is rendering the data correctly. 当前没有什么是$ this-> productID返回0,我肯定它是正确的,因为函数index()依赖于此变量并正确呈现数据。 The ajax request does not appear to be accessing the global variable $this->productID.
Ajax请求似乎没有访问全局变量$ this-> productID。
$.ajax(
{
type: 'GET', // use GET here
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
You are using $this->uri->segment(3);
您正在使用
$this->uri->segment(3);
for galleryID
and $this->uri->segment(4);
用于
galleryID
和$this->uri->segment(4);
for productID
but the url in ajax
call doesn't have these parameters you should pass these ids in ajax call in order to get like 对于
productID
但是ajax
调用中的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);
}
});
And in your class i assume you have define the global variables like 在您的课堂上,我假设您已经定义了全局变量,例如
class checkout extends CI_Controller {
public $galleryID;
public $productID;
// your other code
}
in java-script pass the value js to globle ajax 在Java脚本中将值js传递给globe ajax
$("#abc").on("change",function(){
var post_data = new FormData();
ajax_request("dashboard/ajax",post_data, response,null);
});
and then in JS 然后在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);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.