javascript - 带有 ajax 的 Codeigniter : success function not working

标签 javascript php ajax codeigniter

我的 $.ajax 函数中的成功函数有问题 在 javascript 文件中:

$("#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');
                }

            });

我的 Controller 函数

   <?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'

**

最佳答案

base_url() 是一个 Codeigniter 函数 (php),在你的 $ajax 函数中你使用了 javascript 变量 base_url,它不是定义。

为了让 php base_url() 进入您的 $ajax 函数,您需要回显 php 函数,更改为以下行:

url: "<?php echo base_url() ?>Search_controller/testing_controller",

关于javascript - 带有 ajax 的 Codeigniter : success function not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357540/

相关文章:

php_mongo 扩展已经安装了,但是为什么 rockmongo 仍然没有连接?

javascript - request.get_data()、request.form 等始终为空

javascript - "this"在 React 组件中代表什么? (这个状态..)

javascript - 同时向同一路由发出两个不同的 POST 请求

javascript - 如何使用 jquery 选择具有特定 'alt' 属性的图像

javascript - 如何从gradle调用任意方法?

php - googlemaps 文档类型错误

php - 从 MySQL 检索值

javascript - 无论如何要在单层上指定特征的 z 顺序?

javascript - Redux - 在组件中调用调度操作后解决 Promise