A
A
alex5e2014-07-01 19:11:26
JavaScript
alex5e, 2014-07-01 19:11:26

How to pass model to ajax (Yii)?

Good evening. When passing the model via ajax, I get the error "Object of class Posts could not be converted to string "
In the widget, this is the code:

'<script>
            var data="data="+JSON.stringify('.$this->model.'); // Вот тут ошибка 
            $("#buttonread").click(function(){
            var w = $(".modal-body");
            $.ajax({url:"'.Yii::app()->createUrl('Posts/EditPost').'", type: "POST", data: data})
            });
            </script>';


How to fix, do not tell me?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kfuntov, 2014-07-02
@alex5e

Replace $this->modelwith json_encode($this->model->attributes)
This should fix this error.
But in general, you are doing something completely wrong, the whole code looks strange, very, very.

V
Vit, 2014-07-01
@fornit1917

Did you expect otherwise by concatenating a string and a Posts class object? You won’t be able to transfer the model from PHP to Javascript, as they are different languages ​​in general. You can serialize the model to a string and pass it as a string. How to do it - you decide, depending on what you need.

A
Appp Zooo, 2014-07-01
@ikeagold

I defined the host string:
And then added it to "Posts/EditPost".
If you know a better solution, I'll be glad to like it) otherwise these are such crutches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question