• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a 500 error when using ajax POST

[eluser]Andre Dublin[/eluser]
I'm having trouble returning data from my controller using an ajax post function. What happens here is the user will input a style number, submit that number to the controller. The controller grabs the value submitted, queries the database and returns the results to the controller then the controller returns an array that I then encode into json. But I keep getting a 500 Internal Server Error???

Here is my Controller

//return schools based on style id
public function search() {
    $input = json_decode($this->input->post('obj'));
    $style_id = $input['style_id'];
    $parent_id = $input['parent_id'];
    $data = array();
    if ($q = $this->page_model->search_results($style_id, $parent_id)) {
        $data = $q;
    echo json_encode($data);

Here is my Model

//get all entries
function search_results($style_id, $parent_id) {
    $options = array(
        'Style' => $style_id,
        'Parent_ID' => $parent_id
    $this->db->join('entry', 'pages.Page_ID = entry.Parent_Page_ID', 'inner');
    $q = $this->db->get();

    if ($q->num_rows() > 0) {
        return $q->result();


Here is my javascript

//dress style search
$($searchBtn).click(function(event) {
    var style_id,
         obj = {};
    //get the value of the input fields
    searchVal = event.currentTarget.previousSibling.value;
    parentID = event.currentTarget.parentElement.childNodes[3].value;
    //The object to be passed to the controller
    obj = { 'style_id' : searchVal, 'parent_id' : parentID };
    //POST the values in json notation, return the search results in json notation
    $.post(base_url + 'index.php/page/search',
            function(data) {
    return false;

I am autoloading my models.

Thanks in advance!

Do you have CSRF protection enabled?

[eluser]Andre Dublin[/eluser]
yes, would that be a problem?

[quote author="Andre Dublin" date="1311045361"]yes, would that be a problem?[/quote]

Yes, it would. If CSRF protection enabled, then __EVERY__ POST data should include $this->input->cookie('csrf_test_name') value. Not just the data you want to sent via AJAX. It would be like this:

    $cookie = $this->input->cookie('csrf_test_name');
    echo "[removed]
    $(document)ready(function() {
        $.post('".site_url()."/controller/method', 'name=kaege&csrf;_test_name=$cookie');

BTW, it's csrf_test_name=$cookie, not csrf;_test_name=$cookie
The ";" always appear no matter how many times I delete it Sad

Don't forget of course to make sure you have the right name for your CSRF token, sometimes causes a headache when moving between different CI installations.

[eluser]Andre Dublin[/eluser]
Awesome thanks!

A 500 error is a server error not a CodeIgniter error!


Wow~hello everyone, I am new here, and this is my first post,nice to meet u.

Tera Gold
Buy Tera Gold

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.