带有 ajax 的 Codeigniter:成功功能不起作用

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


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

                }


            });

我的控制器功能


   <?php 


class Search_controller extends CI_Controller{


    public function index(){


    }



    public function testing_controller(){

        $data ="statessssssss";

        echo json_encode($data);

    }


  }

?>

**


问题是代码什么都不做,我不知道是什么问题总是在浏览器日志中返回给我“ajax 问题”


**


Helenr
浏览 179回答 3
3回答

动漫人物

更改您的网址如下url:"<?php&nbsp;echo&nbsp;base_url()?>index.php/Search_controller/testing_controller",我测试了您的代码及其工作文件。

三国纷争

base_url()是一个 Codeigniter 函数 (&nbsp;php&nbsp;),在您的 $ajax 函数中,您使用了未定义的javascript变量 base_url。为了让 php base_url() 进入您的 $ajax 函数,您需要回显 php 函数,更改为这一行:url:&nbsp;"<?php&nbsp;echo&nbsp;base_url()&nbsp;?>Search_controller/testing_controller",

拉莫斯之舞

我发现解决方案是因为 CSRF 安全$("#country select").change(function () {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var country_value= $(this).val();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var data = { /* params&nbsp; */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "country": country_value,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"state": '001'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data[csfr_token_name] = $.cookie(csfr_cookie_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:base_url + "Search_controller/testing_controller",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('done : ' + data);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;error: function (reponse) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('Problem with ajax');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });此代码工作
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript