0

Ok, so I am using a combination of cakephp 1.2, jquery, and ajax with JSON. Here is what I'm doing:

When a person inputs an employee id, I get the result of that employee id, if there is one, and I send it back as a $javascript->object(empInfo). This works fine. I am returning the information to the function, but I can't seem to process it. Everything I have read says to use it as $.each(empInfo, function(). Here is my code:

COMMON.JS

$(document).ready(function() {
$('#emp_num').blur(function() {
        if($(this).val().length != 0) {
            $.ajax({
                type: "POST",
                datatype: "json",
                url: '/ir_employees/getdetails/empId:' + $(this).val(),
                success: function(empInfo) {
                    populateEmployeeInformation(empInfo);
                }
            });
        }
    });
});

function populateEmployeeInformation(empInfo) {
    $.each(empInfo, function() {
        console.log(this);
    });
}

EMPLOYEES_CONTROLLER.PHP

function getdetails() {
    $empId = $this->passedArgs['empId'];
    $this->layout = 'ajax';
    $this->set('empInfo', $this->IrEmployee->find('all', 
                        array('conditions' =>
                                array('IrEmployee.employee_number' => $empId))));

}

GETDETAILS.CTP

<?php
    if((isset($empInfo))){
        echo $javascript->object($empInfo);
    }

?>

When I log it, I get the following (screenshot):

JSON Screen Capture

How can I use the following information properly (this is the "response" from Firebug):

[{"IrEmployee":{"id":"1","employee_number":"xxxxx","last_name":"Doe","first_name":"John","gender":"M","date_hired":"2013-04-09","date_of_birth":"1950-01-01","plant_id":"0"}}]
4
  • why shouting capitals everywhere? "EMPLOYEES_CONTROLLER.PHP" thats awefully wrong as filename... Commented Apr 24, 2013 at 15:18
  • I'm using all caps as a definition of what I am using. It's not the actual file name. file name is employees_controller.php. I wanted it to stand out. Thanks for your concern. Commented Apr 24, 2013 at 15:37
  • well, don't! it only distracts from the issues at hand. Commented Apr 24, 2013 at 15:41
  • Was it answered without someone pitching a fit? Yes it was. This is how I notate my stuff on here. Don't like it, don't look at it. Commented Apr 24, 2013 at 15:43

1 Answer 1

1

Your response is being returned as a string (console.log shows you're looping through each character of the string), and is not being parsed as JSON.

I think this is because you have datatype instead of dataType (note the uppercase T) in the jQuery AJAX options. If you fix this, jQuery should automatically parse the JSON for you.

Alternatively, you could pass the string into JSON.parse (if it exists, if not you should polyfill)

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

1 Comment

Stupid Typos... changing it to dataType instead of datatype gives me the result I am looking for. Thank you very much. Will accept as answer when I can.

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.