0
$.ajax({
        type: 'POST',
        url: '/ci_practice/blog/reviews',
        data: 'res_id='+res_id,
        success: function(data){
            $('#screen-overlay').show(slow);
            $('#screen-overlay').html(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        console.log(xhr.responseText);
        alert(thrownError);
    }
});

This throws me a 500 error. There is a problem in accessing the function reviews as shown by console.log(xhr.responseText); in console.

How should i assign value to url in ajax to perform this in a codeigniter way. I have jQuery in a different file : ajax.js

Will appreciate your response. Thank You.

13
  • a 500 error indicates the target file has an error; check your error log and go from there. Commented Jun 30, 2013 at 8:46
  • wrong path? try with url: <?php echo base_url(); ?>blog/reviews. Is ci_practice your codeigniter project folder? Commented Jun 30, 2013 at 8:52
  • Yes it is unable to access the controller function directly! There is no problem in controller function as i am just echoing out something for test. Is there any syntax i need to follow to put the url correctly in ajax request? Commented Jun 30, 2013 at 8:54
  • @nevermind Yes it is the project folder. Problem is i can't use php tags in a .js file! Commented Jun 30, 2013 at 8:55
  • @Cruze check the $config['csrf_protection'] in config.php and set it to FALSE if it's enabled. Commented Jun 30, 2013 at 8:58

2 Answers 2

1

You should contain the base_url with controller name / then function name you want to access

$.ajax({
    type: 'POST',
    url: '<?php echo base_url(); ?>/controllername/function',
    data: 'res_id='+res_id,
    success: function(data){
        $('#screen-overlay').show(slow);
        $('#screen-overlay').html(data);
    },
    error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.status);
    console.log(xhr.responseText);
    alert(thrownError);
}

});

in you case i think blog is the controller and reviews is a function so it should look like this url: '<?php echo base_url(); ?>/blog/reviews',

For Forbidden Error 403 forbidden error might be due to a faulty .htaccess rewrite you should remove the index.php url you .htaccess file should contain

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Your config.php should contain this

$config['index_page'] = "";

$config['uri_protocol'] = 'AUTO';

Sign up to request clarification or add additional context in comments.

2 Comments

Now i'm getting 403 Forbidden error instead of 500 Server Error!
I don't have index.php in my url as i've used mod_rewrite! But the access to the file is not allowed i don't know!
1

Problem was the CSRF enabled in my config file! But disabling it is a wrong step.

This link : Codeigniter Ajax CSRF Problem actually solved my problem. Using this code :

var post_data = {
            'res_id' : res_id,
            '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
        };

This just adds a CSRF security key which is needed by default by every POST method while submitting due to CSRF Enabled.

2 Comments

As I mentioned, but you said: I tried! No Luck!
Yeah I tried that but it wasn't working by just disabling it :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.