I'm currently working on a dropdown and having a lot of trouble. Its a dependent dropdown and so far I can get the correct data when I make a selection on the first dropdown but I cannot display it on the second dropdown. I know I get the data because I see it while debugging on chrome. This is an Example of what i get:
Object {readyState: 4, responseText: "{"Hardware":1,"Software":2,"Other":3,"option":10}Page generated in 0.2831 seconds.<br><br><br>", status: 200, statusText: "OK"}
parsererror
Object {readyState: 4, responseText: "{"General":5,"Books":6}Page generated in 0.2902 seconds.<br><br><br>", status: 200, statusText: "OK"}
parsererror
This is my javascript code:
<script type="text/javascript">
$(document).ready(function () {
$("#request_department").change(function(){
var data = {
department_id: $(this).val()
};
$.ajax({
type: 'POST',
{#url: '{{ path("select_options") }} ?category_id' + dep,#}
url: "{{ url('select_options') }}?dep_id=" + data.department_id,
data: data,
success: function(data) {
window.alert('hi');
var $option_selector = $('#request_option');
$option_selector.html('<option>Option</option>');
for (var i=0, total = data.length; i < total; i++) {
$option_selector.append('<option value="' + data[i].id + '">' + data[i].option + '</option>');
}
},
error: function(xhr, error){
console.debug(xhr); console.debug(error);
},
});
});
});
</script>
And this the code in the controller:
public function optionAjaxAction(Request $request)
{
if (!$request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
$id = $request->query->get('dep_id');
$result = array();
// Return a list of options, based on the selected department
$repo = $this->getDoctrine()->getManager()->getRepository('MyBundle:RequestOption');
$option = $repo->findByDepartment($id, array('department' => 'asc'));
//var_dump($hardware);
foreach ($option as $o) {
$result[$o->getOption()] = $o->getId();
}
return new JsonResponse($result);
}
Im desperate. I would really appreciate any help! Thanks in advance (:
Page generated in #.#### seconds.is being injected into the final response. Maybe you (or a loaded bundle) have set up a response filter.