Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

javascript - Codeigniter Ajax Call 404

I have a problem with ajax call in codeigniter. This is the javascript code:

$.ajax({
    method: "POST",
    url: MY_CONSTANT + "login/autentificare",
    data: {
        login_email: login_email,
        login_password: login_password
    },
    success: function(result) {
        result = JSON.parse(result);
        console.log(result);
        if (result.errors) {
            if (result.errors.length > 0) {
                swal.fire({
                    text: result.errors,
                    icon: "error",
                    buttonsStyling: false,
                    confirmButtonText: "Ok, got it!",
                    customClass: {
                        confirmButton: "btn font-weight-bold btn-light-primary"
                    }
                }).then(function() {
                    KTUtil.scrollTop();
                });
            }
        } else {
        }
    }
});

And controller function is:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
    public function index() {
        $this->load->view('login/login');   
    }
    
    public function autentificare() {
        die('test');
    }
}

Every time I got 404 error for Ajax call and the url is correct. Can you help me please?

Thank you!!

question from:https://stackoverflow.com/questions/65899439/codeigniter-ajax-call-404

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think the error comes because you are not returning JSON data, in your controller's function

public function autentificare() {
        return json_encode(['response'=>'success']);
 }

I think this will work :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...