2012-06-04 179 views
0
public function actionajaxSearch() { 
     $data_fetched=Person::model()->findByAttributes (array('Code'=>'Cust0001')); 
     echo CJSON::encode($data_fetched); } 



    $('#searchResult').live('pageshow', function(e,info) 
    { 
    $.post('?r=mobile/ajaxSearch',$('form').serialize(), 
    function(res) 
    {   
    arrayvalue =res;   
    $.each(arrayvalue, function(i, profile) { 
     alert(i); 
     alert(profile); 
    }); 
     } 
    }); 

我得到的输出为json编码之一。 在遍历警报我得到的每个字符的值不是按键或值。 有什么帮助吗?ajax json输出解析jquerymobile

+0

你有没有像这样尝试arrayvalue = res.d;并记录阵列值,看看它会帮助你的格式。 – Tamkeen

回答

0

添加数据类型和内容类型解决了问题。为他人的参考添加了完整的代码。

 public function actionajaxSearch() { 
    $data_fetched=Person::model()->findByAttributes (array('Code'=>'Cust0001')); 
    echo CJSON::encode($data_fetched); } 



     $('#searchResult').live('pageshow', function(e,info) 
     { 
     $.ajax({ 
      beforeSend: function() { $.mobile.showPageLoadingMsg(); }, 
      complete: function() { $.mobile.hidePageLoadingMsg() }, 
      url: '?r=mobile/ajaxSearch', 
      data: $('form').serialize(), 
      type: 'POST', 
      ContentType: "application/json", 
      dataType: "json", 
      success:function(res) { 
       if(res !='') 
       { 
       $.each(res, function(key, value) { 
        var li='<li><a href="#">'+value['Code']+'</a></li>'; 
        $("#mylist").append(li); //append li to ul of id list 

       }); //eachfunction 
       $('#mylist').listview(); 
       $('#mylist').listview('refresh'); 
     }//sucess 
     });