yii控制器动作不会从表单中触发

时间:2013-01-09 21:39:38

标签: ajax yii

我正在尝试使用yii进行简单的ajax调用,而不是使用本机yii方法。我的目标是通过在create操作中使用db查找来填充来自邮政编码字段的输入的州和城市字段。我有一个单独的javascript文件,

    $('#Accounts_zip').bind('focusout',function(){     
    $.ajax({
        type: 'POST',
          url:'/gatesadmin/index.php?r=Accounts/Ziplookup',
          data: {zip:$('#Accounts_zip').val()},
          success:  function(datain, textStatus, jqXHR) {alert(datain.statecode);} ,
          error: function(jqxhr, textStatus, errorthrown){alert(errorthrown);},
          dataType:JSON
       });
    alert($('#Accounts_zip').val());
    });

and in my AccountsController I have:

public function actionZiplookup()
{
    $z = new ZipDao();
    $row = $z->GetCityStateByZip($_REQUEST['data']);
    header('Content-Type: application/json; charset="UTF-8"');
    echo CJSON::encode(array('output'=>$row));
}

and my form is out of the box generated CRUD:

    $('#Accounts_zip').bind('focusout',function(){     
    $.ajax({
        type: 'POST',
          url:'/gatesadmin/index.php?r=Accounts/Ziplookup',
          data: {zip:$('#Accounts_zip').val()},
          success:  function(datain, textStatus, jqXHR) {alert(datain.statecode);} ,
          error: function(jqxhr, textStatus, errorthrown){alert(errorthrown);},
          dataType:JSON
       });
    alert($('#Accounts_zip').val());
    });

我知道javascript正在运行,因为我得到了警报,如果我直接调用URL,则会触发actionZiplookup事件,因为我收到了返回的json。我似乎无法成功地获取表单来调用ajax因为我在jquery ajax失败事件中遇到外部服务器错误。我已经梳理了网络和最接近的示例,我可以找到将javascript嵌入到表单本身并使用Yii :: app() - createUrl()方法来定义url,但必须有一种方法来获取控制器在将javascript代码保存在单独的文件中时触发的操作。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

事实证明我在这里找不到的东西可以在这里找到: How to get response as json format(application/json) in yii?这一切都与告诉控制器忽略布局等有关。