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

#1
[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

Code:
//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

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

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

}

Here is my javascript

Code:
//dress style search
$($searchBtn).click(function(event) {
    var style_id,
         parent_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',
            obj,
            function(data) {
                console.log(data);
            },
            'json');
    return false;
});

I am autoloading my models.

Thanks in advance!

#2
[eluser]TheBaron[/eluser]
Do you have CSRF protection enabled?

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

#4
[eluser]kaege[/eluser]
[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:

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

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

#5
[eluser]iain.b[/eluser]
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.

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

#7
[eluser]InsiteFX[/eluser]
A 500 error is a server error not a CodeIgniter error!

InsiteFX

#8
[eluser]Unknown[/eluser]
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.