Has someone been using microAjax (https://code.google.com/p/microajax/)?
I tried to use it in Yii 1.1.x, but I’m getting strange results.
Here is code of a sample action:
public function actionChangeState($id, $type)
{
if(Yii::app()->request->isAjaxRequest) echo('ajax'); else echo('non-ajax');
Yii::app()->end();
}
and here are three different ways (microAjax, jQuery and yiiGridView update) I’ve been testing.
microAjax(url, function(result)
{
alert('microAjax: ' + result);
});
$.ajax
({
type: 'GET',
url: $(this).attr('href'),
}).done(function(result)
{
alert('$.ajax: ' + result);
});
$.fn.yiiGridView.update('".$grid."',
{
type:'GET',
url:$(this).attr('href'),
success:function(result)
{
alert('$.fn.yiiGridView: ' + result);
}
});
Both jQuery and yiiGridView update are fine, returning ‘ajax’ as a result. Only microAjax fails, returning ‘non-ajax’.
Does anybody have any idea, what is wrong?
EDIT: Example from microAjax FAQ of detecting on server side, if we deal with an AJAX request:
if ($_SERVER['X-Requested-With'] == 'XMLHttpRequest') {
// do something clever
}
seems to be pretty the same as Yii uses:
public function getIsAjaxRequest()
{
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest';
}
The only difference, I see, is that X-Requested-With is capitalized and prepended by HTTP in case of Yii and non in case of microAjax. Can this be source of the problem? microAjax has been updated last time about five years ago. Does something significant changed during this time in the area of detecting ajax requests in PHP?