Yii Framework Forum: Yii Ajax problem - Yii Framework Forum

Jump to content

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

Yii Ajax problem Rate Topic: -----

#1 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 16-December 09

Posted 26 October 2011 - 04:05 AM

Hello..
I have following problem and I don't know how to solve it.

I have table with lots of rows that can be dynamically added/removed by jQuery. It works. But on each row of this table there are 2 drop-down-lists. If I choose something in the first one, the second one should be refreshed and filled with new values using ajax. Typical task. But it does not work as I want.

Because all rows have the same structure I created a function that creates code for a new row. This function is in controller. If I use it, Yii does not generate jQuery code and my ajax does not work.
If I try to define one set of drop-down-lists manually in the view, it works well.

I wanted not to use the Yii's ajax and to write it by my self. It is easier than the Yii's way, but it does not work neither. I did it like this:

echo CHtml::dropDownList(
'name',
'',
array(1=>'val1',2=>'val2'),
array('onChange'=>'$.post(\''.Yii::app()->request->baseUrl.'/controller/action\');')
);


this produces following HTML code:

select id="name" name="name" onchange="$.post('/mysite/controller/action');">


And when I select a value in the drop-down-list I only get CHTML error 400 = bad request?
The post() function does not work.

Im not able to describe my problem better, it is very complacated .. I hope someone has solved a similar problem..
0

#2 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 26 October 2011 - 05:47 AM

Try to launc the script alone:
$.post('/mysite/controller/action');


Maybe you need
$.post('controller/action');
(*without the first slash that means http://www.yoursyte....ntroller/action)

But in this way, you dont send anything: if you want to send data you can use some ways:

$.post("test.php", { name: "John", time: "2pm" } );


or also

$.post("test.php", $("#testform").serialize());


(you can find more documentation at http://api.jquery.com/jQuery.post/)
0

#3 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 16-December 09

Posted 26 October 2011 - 07:57 AM

Hi. Thanks for response. I already tried all you mentioned but it did not work. I really do not know where can be the problem.
On the internet I found that error 400 =
The request could not be understood by the server due to malformed syntax

But when I open Firebug I see that script calls existing action and if I enter it's address to browser, it works.

Now I found anothet error message:
The CSRF token could not be verified.

It maybe an error due to some safety in Yii.
I will have look at it.

----

My drop down box looks like this. It has to be in form to be able to send POST variable:

<form method="post" action="/website/controller/action">
  <div style="display:none">
    <input type="hidden" name="YII_CSRF_TOKEN" value="xxx">
  </div>
  <select id="mySelect" name="mySelect" onchange="$.post('/mysite/controller/action');">
    <option value="1">has value I</option>
    <option value="2">has value II</option>
  </select>
</form>


I tried to add to form attribute:
csrf="1"

... but no change.
0

#4 User is offline   marco@sms 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-August 11
  • Location:Hamburg, Germany

Posted 26 October 2011 - 08:09 AM

Does this help?
http://www.techporta...i-framework/219



Quote

Question: Getting error: Bad Request The CSRF token could not be verified: when CSRF has been enabled in the Yii Framework and I'm clicking on a linkButton within a form.

Answer:
Add 'YII_CSRF_TOKEN' => Yii::app()->request->csrfToken to the params array in the linkButton




0

#5 User is offline   marco@sms 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-August 11
  • Location:Hamburg, Germany

Posted 26 October 2011 - 08:10 AM

http://willowdan.blo...-could-not.html

Quote

Error: Bad Request The CSRF token could not be verified
Just got the above error while submitting a form in my Yii powered SaaS app. I realized that the form isn't generated by Yii, but was customized. Hence I had to add a hidden field:

echo CHtml::hiddenField('YII_CSRF_TOKEN',Yii::app()->request->csrfToken);



0

#6 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 16-December 09

Posted 26 October 2011 - 08:14 AM

Tadaaa !! Success

This error occured because I turned form-security settings on in protected/config/main.php:



'components'=>array(
'request'=>array(
'enableCsrfValidation'=>true,
...


Every form than has to contain (and automatically contains) this field:

<input type="hidden" name="YII_CSRF_TOKEN" value="xxx">


Problem is that if you want to send something via POST, you have to send also this field.

In case of $.post() you MUST add at least one variable (named YII_CSRF_TOKEN) like this:

onchange="$.post('/mysite/controller/action',{'YII_CSRF_TOKEN':'xxx'});"
// mysite = Yii::app()->request->baseUrl
// xxx = Yii::app()->request->csrfToken


If you do it like this, you do not need to use a form and serialize it. Yii's Ajax puts it in a form and serializes it because of the YII_CSRF_TOKEN field.
0

#7 User is offline   marco@sms 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-August 11
  • Location:Hamburg, Germany

Posted 26 October 2011 - 08:22 AM

Thats what I wrote^^
:rolleyes:
0

#8 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 16-December 09

Posted 26 October 2011 - 08:45 AM

Actually.. The main problem was not solved. Yii's default Ajax doesn't work. Now works only my manual approach.
When I use Yii's ajax (drop down list) it works only if there is not defined any <script> section the same view.

Does anybody know the reason?

PS: And what's funny - if the drop down list created using Yii is present on the page, no java scripts work at all.
0

#9 User is offline   marco@sms 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-August 11
  • Location:Hamburg, Germany

Posted 26 October 2011 - 08:51 AM

Please post the generated source that does NOT work.
0

#10 User is offline   rackycz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 16-December 09

Posted 26 October 2011 - 09:34 AM

So when I was preparing the code for you, I foud out that when I was specifying "src" for js:
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/ ... " ></script>

... i didn't add a "dot" infront of path. Because Yii::app()->request->baseUrl returns "/website/" and Yii's Ajax than crashed. So I added the dot like this:
<script type="text/javascript" src=".<?php echo Yii::app()->request->baseUrl; ?>/ ... " ></script>


But firebug marked it as a mistake.

Second thing is something I still do not understand. I have a text box that should be driven by "datetimepicker" like this:

$("#textboxId" ).datetimepicker({
// properties
});


But browser does not like the .datetimepicker
If I remove the Ajax drop down list, everything works well..

-----

And script added by Yii's ajax looks cca like this. (I reformated the code to be more readable) Online validator says that there is missing a semicolon. I am not sure.

<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) 
{
    jQuery('body')
      .undelegate('#myDropDown','change')
      .delegate('#myDropDown','change',function()
       {
            jQuery.ajax({'type':'POST',
                          'url':'/mysite/controller/action',
                        'cache':false,
                         'data':jQuery(this).parents("form").serialize(),
                      'success':function(html)
                                {
                                 jQuery("#updatedDropDownList").html(html)
                                }
                       });
           return false;
        });
});
/*]]>*/
</script>



Yii adds this code to the end of body. If I open generated HTML source of my webpage and copy this script to the top of my view (above all <script> sections), ajax works. But still the rest of javascript don't.

-----

Yii's Ajax sucks I think.. I will do I my way.
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