CActiveForm 1.1.7 ajax using jsonp

Hi all,

I have updated my project to use Yii 1.1.7 and I have noticed that the activeform javascript on ajax validation is using jsonp method as it is adding a ?callback=jQuery… parameter to its ajax requests.

This then breaks the because the return type is plain json generated by CActiveForm::validate($model)

I can make this work by adding.




if (isset($_POST['ajax']) && $_POST['ajax'] === 'mymodel') {

	echo $_REQUEST['callback'] . '(';

	echo CActiveForm::validate($m);

	echo ')';

	Yii::app()->end();

}



this wraps the returned json in the callback name like:




jQuery151017513468124114617_1302282226873(

{"AuthItem_name":["An item with the name \"guest\" already exists."]}

)



This then fixes the issue.

But i can’t figure out why it is posting using jsonp. I have noticed that a lot has changed in ajax in the new release of jquery.

Also my CActiveForm widget only has a very small configuration:




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'authitem',

	'enableAjaxValidation'=>true,

	'focus'=>array($model,'name'),

)); ?>



I am not specifying the url so it should use the same as the current page. Very Confused. If anyone knows anything about this would be a big help.

No, we are using ‘json’, not ‘jsonp’.

Hi qiang,

I know, looking through yiiactiveform.js was the first thing I did to see if there was something I was missing.

It clearly sets up the ajax to use json.

However it was definitely making jsonp style calls. Then I noticed a few other ajax calls in the application also trying to use jsonp.

I think I have finally figured out what is causing it. If I turn off the ajaxqueue plugin everything work the way it should. A bit more digging revealed http://plugins.jquery.com/content/ajaxqueue-json-calls-not-working-jquery-151 Aparently the ajaxqueue js file shipped with 1.1.7 is not compatible with jquery 1.5.1

I have the same issue like above.

I have a form with an ajax call which just call an action which returns valid json code.

controller code:




public function actionTestAjax()

{

  echo CJSON::encode(array('success'=>true));

  Yii::app()->end();

}



view code:




$ajaxurl=Yii::app()->getUrlManager()->createUrl('/post/testajax');

echo CHtml::link('testajax', $ajaxurl,$htmlOptions=array('alt'=>'testajaxbutton' ,'class'=>'testajaxbutton'));


$js=<<<EOP

function testajax(){

    var jqxhr = $.ajax(

    {

        type: "POST",

        //dataType:"text json",

        /*

        cache:false,

        async:false,

        jsonp:false,

        callback:null,

        */

        dataType:"json",

		url: $(this).attr('href'),

        statusCode: {

            404: function() {

             alert('page not found');

            }

        },

        success: function(data, textStatus, jqXHR){

           alert('success within ajax funktion');

        },

        error:function(jqXHR, textStatus, errorThrown){

           alert('error within ajax funktion');

        },

        complete:function(jqXHR, textStatus){

            alert('complete within ajax funktion');

        },

    });

return false;

}

$(".testajaxbutton").bind("click",testajax);

EOP;


Yii::app()->getClientScript()->registerScript('testajax', $js);



And it works as it should! The ajax url is /demos/blog/index.php/post/testajax

But when i also add a CAutocomplete to that form the urls from the testajax ajax call are added a callback.

/testajax?callback=jQuery15206772807063900166_1302594566603&_=1302594758360

So jsonp is used.

sadly if i set jsonp:false, and callback:null, in the ajax call the urls are correct again but i still get the error invalid label in firebug.

Thanks for the solution with jsonp: null, and jsonpCallback: null my ajax call is indeed also working again. I guess i thought that false would be the correct setting for jsonp.

This is indeed the troublemaker.

For me this also worked if my ajax controller function generated the jsonp callback function like below.




 public function generate_jsonp($data) {

  if (preg_match('/\W/', $_GET['callback'])) {

    // if $_GET['callback'] contains a non-word character,

    // this could be an XSS attack.

    header('HTTP/1.1 400 Bad Request');

    exit();

  }

  header('Content-type: application/javascript; charset=utf-8');

  print sprintf('%s(%s);', $_GET['callback'], json_encode($data));

}


public function actionTestAjax()

{

$this->generate_jsonp(array('error'=>'this is an error'));

}



I also wonder when i use the jquery-ajax-queue from below everything works fine.

http://plugins.jquery.com/files/jquery-ajax-queue_1.0.js.txt

Which one is correct the file coming with yii or the above (even if its old)?

Hi,

I had the same error.

An ideea could be the incompatibility between jQuery 1.5 and form validator (if you use the one found in link below).

The bug is shown here and also you’ll find a patch:

validator incompatibility explained

Cheers,

Paul