我目前正在研究一個下拉菜單並且遇到了很多麻煩。它是一個依賴下拉列表,到目前爲止,我可以在第一個下拉列表中選擇正確的數據,但無法在第二個下拉列表中顯示它。我知道我得到的數據,因爲我看到它在調試鉻。這是我所得到的一個例子:Json parsererror in dropdown
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
這是我的javascript代碼:
<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>
而且在控制器上的這個代碼:
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);
}
林絕望。我非常感謝任何幫助!在此先感謝(:
你需要找出'#中生成的頁面#### seconds.'被注入到最終的響應。也許你(或者一個已加載的包)已經設置了一個[響應過濾器](https://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html#after-filters-with-the-kernel-response-event )。 – Yoshi