[英]Codeigniter with ajax: success function not working
I have a problem with success function in my $.ajax function in javascript file:我的 $.ajax function 文件中的 function 成功有问题:
$("#country select").change(function () {
var country_value = $(this).val();
$.ajax({
url:base_url + "Search_controller/testing_controller",
method: 'post',
data: {country_val: country_value },
dataType: 'json',
success: function(data){
console.log('done : ' + data);
},
error: function (reponse) {
console.log('Problem with ajax');
}
});
my Controller function我的 Controller function
<?php
class Search_controller extends CI_Controller{
public function index(){
}
public function testing_controller(){
$data ="statessssssss";
echo json_encode($data);
}
}
?>
** **
The Problem is the codes do nothing, i don't know what the problem Always return to me in Browser log 'Problem with ajax'问题是代码什么都不做,我不知道是什么问题总是在浏览器日志中返回给我“ajax 问题”
** **
base_url()
is a Codeigniter function ( php ), in your $ajax function you use the javascript variable base_url, which is not defined. base_url()
is a Codeigniter function ( php ), in your $ajax function you use the javascript variable base_url, which is not defined.
in order to get the php base_url() into your $ajax function you need to echo out the php function, changing to this line: in order to get the php base_url() into your $ajax function you need to echo out the php function, changing to this line:
url: "<?php echo base_url() ?>Search_controller/testing_controller",
Change your URL as follows更改您的 URL 如下
url:"<?php echo base_url()?>index.php/Search_controller/testing_controller",
I tested your code and its working file.我测试了您的代码及其工作文件。
I find the solution is because of CSRF security我发现解决方案是因为 CSRF 安全性
$("#country select").change(function () {
var country_value= $(this).val();
var data = { /* params */
"country": country_value,
"state": '001'
};
data[csfr_token_name] = $.cookie(csfr_cookie_name);
$.ajax({
url:base_url + "Search_controller/testing_controller",
method: 'post',
data: data,
dataType: 'json',
success: function(data){
console.log('done : ' + data);
},
error: function (reponse) {
console.log('Problem with ajax');
}
});
This Code Working此代码有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.