select2 problem with onChange

select 2 extension is a great extension.

I wanted it to do a query from controller as user select an option from it.

but I got a technical issue with setting "onChange" html options.

the problem is with accessing ‘data’ property in jQuery.

here’s my view code:


<?php $this->widget('ext.select2.ESelect2', array(

'model'=>$model,

'attribute'=>'templateId',

'data'=>$model->allSubjectTemplates,

'options'=>array(

	'placeholder'=>'saved message templates...',

	'allowClear'=>true,

	'ajax'=>array(

		'url'=>$this->createUrl('sendMessage/getTemplateBody'),

		'update'=>'#SendDedicateMessageViewModel_context',

		),

	),

'htmlOptions'=>array(

	'style'=>"width:250px;",

	'onChange'=>CHtml::ajax(array('type'=>'POST',

		'url'=>'sendMessage/GetTemplateBody',

		'data'=>'js:$("#SendDedicateMessageViewModel_templateId").serialize()',

		'beforeSend'=>'function(){

		$("img", parent).css("display", "inline");

		}',

		'success'=>'function(data)

		{


		[b]console.debug(data.subject);

		console.debug(data);

		console.debug(data.subject);[/b]

		js:$("#SendDedicateMessageViewModel_subject").attr("value",data.subject);

		

		js:$("#SendDedicateMessageViewModel_context").text(data.context);

		

		}',

		'complete'=>'function(){

		$("img", parent).css("display", "none");

		}',

		)),

	),

));?>

bold lines in firebug appears as:


undefined

{"typ":false,"subject":"Top Secret","context":"This message can not be copy/pasted or snapshot","all":"4"}

undefined

any idea people?

// when I tried replacing "data.subject" and "data.context" with a simple string. it appears properly in browser

Hi dear ali,

The JSON before was JSON just is a String :wink:

You can not access to JSON in javascript directly.

You must use JSON.parse and be like this command. (+)

But in jQuery, you can use jQuery.parseJSON( json ) be simple! (+)

So, You must change your code be like this:


var data = jQuery.parseJSON(data);

console.debug(data.subject);

The best regards

Nabi