基本於Yii 的ajax json請求 接收處理

來源:互聯網
上載者:User

(原文:http://lei3056.blog.163.com/blog/static/39532279201181105142902/)

CGridview:

array(

'name'=>'remark',

'type'=>'raw',

'value'=>'CHtml::textField("remark",$data->remark,array(

"size"=>7,

//"readonly"=>"readonly",

"class"=>"textfix",

"id"=>"$data->id",

))'

),

這段落了哈。現在補上。


view:未輸出的

Yii::app()->clientscript->registerScript('posiiton','

    jQuery("#person-grid .textfix").live("change",function() {//jquery 的 live 方法實在js用得多的網站不能少了

        if(!confirm("確定要fix這條資料嗎?")) return false;

        var id = $(this).attr("id");

        var val = $(this).val();

        $.ajax({

            type:"POST",

            dataType:"json",//dataType (xml html script json jsonp text)

            data:{"id":id,"val":val},//json 資料

            url:"'.Yii::app()->createUrl("/admin/person/updateremark/").'",//YII 的產生地址

            success:function(json) {//成功獲得的也是json對象

                //$.fn.yiiGridView.update("ad-grid");

                alert(json.val);//對象中的val屬性

                $(this).val(json.val);

                //alert("success")

            }

        });

        return false;

    });

');

html:輸出最後的結果

jQuery("#person-grid .textfix").live("change",function() {

if(!confirm("確定要fix這條資料嗎?")) return false;

var id = $(this).attr("id");

var val = $(this).val();

$.ajax({

type:"POST",

dataType:"json",

data:{"id":id,"val":val},

url:"/admin.php?r=admin/person/updateremark",

success:function(json) {

//$.fn.yiiGridView.update("ad-grid");

alert(json.val);

$(this).val(json.val);

//alert("success")

}

});

return false;

});

controller:接收json ajax 請求

public function actionUpdateRemark(){

if(Yii::app()->request->isAjaxRequest){//是否ajax請求

$id =  (int)Yii::app()->request->getParam('id');//getparam 會獲得 get post 變數 ,原來也可以接收json處理後的變數

$val = Yii::app()->request->getParam('val');

$model = $this->loadModel($id);

$model->remark = $val;

if($model->save()){

echo CJSON::encode(array('val'=>$model->remark));//Yii 的方法將數組處理成json資料

}

}

}

 

之前我的另一篇關於CJSON的文章

補充一下,在編輯都在UTF-8的環境下,沒有亂碼問題。

在IE下邊的JS資料轉送上,JQUERY 和YII 都做得很好,如果用GET的話就出現中文問題,用JSON沒有問題。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.