Ajax json request Receiving and Processing Based on Yii

Source: Internet
Author: User

(Original: 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 ",

))'

),

This paragraph is correct. Make up now.


View: unoutput

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

JQuery ("# person-grid. textfix"). live ("change", function () {// jquery's live method is actually a lot of JavaScript websites.

If (! Confirm ("are you sure you want to fix this data? ") 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 data

Url: "'. Yii: app ()-> createUrl ("/admin/person/updateremark/").'", // address generated by YII

Success: function (json) {// json object is also obtained successfully

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

Alert (json. val); // val attribute in the object

$ (This). val (json. val );

// Alert ("success ")

}

});

Return false;

});

');

Html: output the final result

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

If (! Confirm ("are you sure you want to fix this data? ") 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: receives json ajax requests

Public function actionUpdateRemark (){

If (Yii: app ()-> request-> isAjaxRequest) {// whether to request ajax

$ Id = (int) Yii: app ()-> request-> getParam ('id'); // getparam will get the get post variable, you can also receive json-processed variables.

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

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

$ Model-> remark = $ val;

If ($ model-> save ()){

Echo CJSON: encode (array ('val '=> $ model-> remark); // The Yii method processes the array into json data

}

}

}

 

My other article on CJSON

In addition, the editing are all in the UTF-8 environment, there is no garbled problem.

In JS data transmission under IE, JQUERY and YII are both doing well. If GET is used, there will be a problem in Chinese, and there will be no problem in JSON.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.