Yii Framework Forum: Call js function from jQuery ajax callback - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Call js function from jQuery ajax callback Rate Topic: -----

#1 User is offline   Timbo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 19-January 10

Posted 19 January 2010 - 09:12 AM

I'm a total newbie to ajax and jquery. I have got my first ajax sample working, but I have now run into a block.

I want to call a function from within a success function in an ajax call. But it always comes up with a js error "Object expected".

Here is the view code:
<script type="javascript">
function test(val) {
    alert('val = '+val);    
}
</script>

<?php echo CHtml::beginForm(); ?> 
<?php echo CHtml::label('Seed Number:','seed'); ?>
<?php echo CHtml::textField('seed',$seed,array('id'=>'seed','size'=>4,'maxlength'=>4)); ?>
<p>
Square: <span id="sq"><?php echo $sq; ?></span><br />
Square root: <span id="sqrt"><?php echo $sqrt; ?></span>
</p>
<?php echo CHtml::ajaxSubmitButton('Calculate',CController::createUrl('game/test'),
    array('dataType'=>'json',
          'success'=>'function(data,status){ test("abc"); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}'));    
?>
</form>


The call to test ('test("abc");') causes the problem.

How does one call a function like this?
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 19 January 2010 - 09:58 AM

this line:
 'success'=>'function(data,status){ test("abc"); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}')); 

should be
 'success'=>'js:function(data,status){ test("abc"); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}')); 

Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#3 User is offline   Timbo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 19-January 10

Posted 19 January 2010 - 11:02 AM

View PostPoL, on 19 January 2010 - 09:58 AM, said:

this line:
 'success'=>'function(data,status){ test("abc"); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}')); 

should be
 'success'=>'js:function(data,status){ test("abc"); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}')); 


Thanks Pol.

I tried it, but still the same result: "Object expected". The page source is identical.
0

#4 User is offline   ricardograna 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 376
  • Joined: 31-March 09
  • Location:Manaus/AM - Brazil

Posted 19 January 2010 - 01:45 PM

Are your controller rendering the result with json_encode?
Yes, It Is!
0

#5 User is offline   Timbo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 19-January 10

Posted 19 January 2010 - 02:52 PM

View Postricardograna, on 19 January 2010 - 01:45 PM, said:

Are your controller rendering the result with json_encode?

Yes - it's only the call to the js function test(). If that is removed it works just fine - even if I replace it with alert().

Here's the controller:
    public function actionTest() {
        $seed = intval('0'.$_POST['seed']);
        if ($seed > 0) {
            $sq = pow($seed, 2);
            $sqrt = round(sqrt($seed), 3);
        }
        else {
            $seed = 0;
            $sq = 0;
            $sqrt = 0;
        }
        if(Yii::app()->request->isAjaxRequest) { 
            $json = CJavaScript::jsonEncode(array('sq'=>$sq,'sqrt'=>$sqrt));
            echo $json;
            Yii::app()->end();
        }
        $this->render('test',array('seed'=>$seed,'sq'=>$sq,'sqrt'=>$sqrt));  
    }

0

#6 User is offline   Timbo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 19-January 10

Posted 20 January 2010 - 04:19 AM

I've also tried to access a global variable, also with no success:

<script type="javascript">
var globalVar = "Global Var";
</script>

...

<?php echo CHtml::ajaxSubmitButton('Calculate',CController::createUrl('game/test'),
    array('dataType'=>'json',
          'success'=>'function(data,status){ alert(globalVar); $("#sq").text(data.sq ); $("#sqrt").text(data.sqrt );}'));    
?>


I've tried all sort of references to globalVar, such as document.globalVar, $(document).globalVar, $(this).parents("document").globalVar, etc, but no luck.

I could of course set and get the value of a hidden element but that does not help me wanting to execute common code external to the jQuery ready() function.

This has kept me busy for days: has anyone done this before?
0

#7 User is offline   Timbo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 19-January 10

Posted 20 January 2010 - 05:48 AM

I have solved the problem: I wrongly defined <script type="javascript"> instead of <script type="text/javascript">.

Grrr...
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users