ajax无法调用codeigniter的功能

时间:2019-06-03 10:04:02

标签: ajax codeigniter

This is Welcome controller method 

    public function send_otp()
    {
        echo 'in';
      die;
        $phone = $_POST['mobile'];
        if ($phone != '') {
            $mobile_detail = $this->welcome_model->check_if_already_mobile_no($phone);
            if (!empty($mobile_detail)) {
                if ($mobile_detail['is_verified'] == 'yes') {
                    $message = 'Already Verified.';
                    echo json_encode(array('status' => 'error', 'message' => $message));
                    exit;
                } else {
                    $this->welcome_model->delete_mobile_no($phone);
                }
            }

            $otp = self::generateRandomNo();
            $this->welcome_model->insert_mobile_detail($phone, $otp);
            $link = file_get_contents("http://49.50.67.32/smsapi/httpapi.jsp?username=aplusv&password=aplusv1&from=APLUSV&to=$phone&text=$otp&coding=0");
            $status = '';
            if ($link != '') {
                $status = 'success';
                $message = 'Successfully Otp send to your no.';
            } else {
                $status = 'error';
                $message = 'Error in sending OTP.';
            }
            echo json_encode(array('status' => $status, 'message' => $message));
            exit;
        }
    }
This is model 
 public function check_if_already_mobile_no($mobile_no = null)
    {
        $query = $this->db->get_where('mobile_sms', array('mobile_no' => $mobile_no));
        return $query->row_array();
    }

    public function get_mobile_details($mobile_no = null, $otp = null)
    {
        $query = $this->db->get_where('mobile_sms', array('mobile_no' => $mobile_no, 'otp' => $otp));
        return $query->row_array();
    }

    public function insert_mobile_detail($phone, $otp)
    {
        $this->mobile_no = $phone;
        $this->otp = $otp;
        $this->is_verified = 'no';
        $this->created_at = date('Y-m-d H:i:s');

        $this->db->insert('mobile_sms', $this);
    }
This is view 
     <div class="container" style="margin-top: 25px;">
            <div class="row">
            <div class="col-md-12" id="response_msg"></div>
                <div class="col-md-4" id="enter_mobile">
                    <form method="POST" action="#">
                        <div class="form-group">
                            <label for="phone">Phone </label>
                            <input type="text" class="form-control" id="mobile" name="phone" placeholder="Enter Mobile">
                        </div>
                        <button type="button" name="send_mobile" id="send_otp" class="btn btn-primary">Submit</button>
                    </form>
                </div>
<script src="assets/js/jquery.js"></script> 
        <script type="text/javascript">
   // var base_url = "<?php echo base_url();?>";
</script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script>
            $(function () { // start of doc ready.
                $("#send_otp").on('click', function (e) {
                    var mobile = $('#mobile').val();
                    alert(mobile);

                    $.ajax({
                url: '<?php echo site_url('index.php/welcome/send_otp'); ?>', 
                        data: {'mobile': mobile}, 
                        type: "post",
                        dataType: 'json',
                        success: function (data) {

                            if (data.status == 'success') {
                                $('#response_msg').html('<div class="alert alert-success" role="alert">' + data.message + '</div>');
                                $('#mobile_no').val(mobile);
                                $('#enter_mobile').hide();
                                $('#verify_otp_form').show();
                            } else {
                                $('#response_msg').html('<div class="alert alert-danger" role="alert">' + data.message + '</div>');
                            }
                        }
                    });

                });
Ajax中的

没有被调用,即$ .ajax在这里不工作,我的控制器(即,使用send_otp方法欢迎)在这里没有被调用。 为什么我的控制器功能没有被调用 如何解决问题 使用base_url调用控制器函数的正确方法是什么 我已经检查了控制台,它也没有显示任何错误

1 个答案:

答案 0 :(得分:0)

  1. 我注意到您使用site_url()的方式略有错误:不要在此处编写index.php,只需使用site_url('welcome/send_otp')
  2. 别忘了在die()方法的顶部有一个无条件的send_otp-它会阻止下面的任何代码执行