2012-06-04 111 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 
     });