doutanggun9816 2014-09-25 15:03
浏览 40
已采纳

如何调用从php json编码传递的ajax数组? zend框架2

I can't call an arrays passed from the php json encode. I would concatenate 2 select state-contry-city. How can I do this??

This is the code:

I have 2 selects. I send the first value to an action:

php action into my controller:

  public function regioniProvinceComuniAction(){

         $response = $this->getResponse();
         $request = $this->getRequest();

         if ($request-> isPost()){
            $post_data = $request->getPost();
            $regione = (int)$post_data['regione'];
            $province = (int)$post_data['province'];                
         }

        if(isset($regione)){
            $prov = $this->getProvincia($regione);                
        }

         if(isset($province)){
            $com = $this->getComuni($province);                
        }

          $response->setContent(\Zend\Json\Json::encode(array('prov' => $prov, 'com' => $com)));
          return $response;    
     }

This is my js file:

  $("select#Regione").change(function () {
       var regione = $("select#Regione option:selected").attr('value');
       $.ajax({
           type: 'POST',
           url: '/zf-tutorial/public/regioni-province-comuni',
           datatype: 'json',
           data: { regione: regione },
           success: function (data) {

           alert (data);  
           }               
       });
  });

An example of the alert function is like this:

{"prov":[{"id":"10","nome":"Genova"},{"id":"8","nome":"Imperia"},{"id":"11","nome":"La Spezia"},{"id":"9","nome":"Savona"}],"com":[]}

I tried to populate my second select via data.post like this, but the values are "undefined":

success: function (data) { 
             $('select#Provincia option').each(function(){$(this).remove()});
             for(var i=0; i<data.length; i++){
                 $("select#Provincia").append('<option value="' + data[i].prov.id + '">' + data[i].prov.nome + '</option>');
             }

i tried to do

for (var i = 0; i < data.prov.length; i++) {
                    $("select#Provincia").append('<option value="' + data.prov[i].id + '">' + data.prov[i].nome + '</option>');
                }

if i set manually the var data, works fine, but with the response data doesn't work. i tried to do:

var prov = data.prov;
$("div#id").html(prov);

but it doesn't work. works only like this:

$("div#id").html(data);

and the output is the same

{"prov":[{"id":"10","nome":"Genova"},{"id":"8","nome":"Imperia"},{"id":"11","nome":"La Spezia"},{"id":"9","nome":"Savona"}],"com":[]}
  • 写回答

1条回答 默认 最新

  • douzhuozhu9544 2014-09-25 15:09
    关注

    You need to loop through the items in data.prov. Change your for loop to this:

    for (var i=0; i<data.prov.length; i++){
        $("select#Provincia").append('<option value="' + data.prov[i].id + '">' + data.prov[i].nome + '</option>');
    }
    

    jsFiddle Demo

    EDIT

    Solution found in comments: Can you try to change datatype in your $.ajax() call to dataType. I'm not sure if the case is important. Sounds like data is a string and not an object

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧