I
I
iamsaint2012-02-20 16:15:56
JavaScript
iamsaint, 2012-02-20 16:15:56

Yii+Jquery $.post?

Good afternoon.

I'm trying to send data to the server using the $.post

method

<script>
    $('#add_element_btn').click(function()
    {
      $.post("<? echo Yii::app()->baseUrl; ?>/catalog/additem/", {
        uuid: <? echo $catalog->uuid; ?>,
        name : $('#input_name').val()
      },
        function(data){
     					alert(data.status); 
      }, "json");
    });
  </script>


Controller
<?<br>
class CatalogController extends CController<br>
{<br>
<br>
  public function actionAdditem()<br>
  {<br>
<br>
  //	$catalog = Catalogs::model()->findbyPk(1);<br>
<br>
    print json_encode(array("status"=>'ok'));<br>
  }<br>
}<br>
?><br>


In this form, alert(data.status); works. But it stops if you remove the comment from the line
// $catalog = Catalogs::model()->findbyPk(1);

Tell me, please, what could be the problem?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
I
Ivan Kryak, 2012-02-20
@sck_v

Instead of "<? echo Yii::app()->baseUrl; ?>/catalog/additem/" it's better to use CController::createUrl('catalog/additem')

E
Eugene, 2012-02-20
@Nc_Soft

$catalog = Catalogs::model()->findbyPk(1);
generates some kind of output, it's obvious

S
Sergey, 2012-02-20
Protko @Fesor

1) Although this is not related to your question, use registerScript to publish JS.
2) Use relative paths in JS - you're better off.
3) To see what's wrong, open Firebug/Dragonfly or whatever it is with your hand on the network tab and see the response from the server.

A
Anatoly, 2012-02-20
@taliban

Have you tried to see what comes from the server when you uncomment it? Or do you like to guess at random what the problem is? We take _any_ browser and look at the result of Ajax, without any alerts and such nonsense, we see the result and we don’t have to ask what the problem is. Like little children, by God.

D
d4rkr00t, 2012-02-20
@d4rkr00t

And the model is exactly called Catalogs and just try to follow the url that the request sends, does it return errors

D
DoooMer, 2014-01-04
@DoooMer

->findbyPk(1);
I see a typo here. Try ->findByPk(1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question