为了设置两个依赖的下拉列表,我使用 jQuery.ajax,但我遇到了一些麻烦,我认为我在 $.ajax 上设置的 Controller 操作的 url 无法访问。
这是我的 Controller 操作代码:
public function fillIncidentsAction()
{
$request = $this->getRequest();
if ($request->isPost())
{
$code_categ = (int) $request->getPost('code_categ',0);
$data = new JsonModel(array(
'success' => true,
'results' => $this->getTableInstance('TypeIncidentTable')
->getListTypeIncident($code_categ),
));
return $data;
}
}
这是我的 js 函数的主要部分
$('#'+source).change(function() {
if($('#'+source).val() != '')
{
$.ajax({
type: 'POST',
async: false,
url: url,
cache: true,
dataType: 'json',
data: { code_categ: $('#'+source).val() },
success: function(data){
alert(data.success);//<------ i added this to test if this function is executed or not
if(data.success)
{
$('#'+target).prop('disabled', true);
if(data.results != ""){
var options = new Array();
$.each(data.results, function(key, value){
options[((key) ? key : 0)] = '<option value="' + key + '">' + value + '</option>';
});
$("#"+target).html(options.join(''));
$('#'+target).prop('disabled', false);;
}
}
},
});
我不知道我错在哪里。有什么建议么? 感谢您的帮助!
抱歉,这就是我设置网址的方式:
<script type="text/javascript">
$(document).ready(function() {
var url = "<?php echo $var =$this->url('gims/default', array('controller' => 'evenement', 'action' => 'fill_incidents')); ?>";
// initialize the js function
dependentDropDown('firstList','secondList',url);
});
</script>
希望这能为您提供更多详细信息。
最佳答案
问题出在我的module.config.php中,我没有启用Json策略。
'view_manager' => array(
//...
'strategies' => array(
'ViewJsonStrategy',
),
),
关于javascript - ZF2-无法在 zend Framework 2 上使用 jQuery.ajax 访问 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229057/