Answer the question
In order to leave comments, you need to log in
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>';
Answer the question
In order to leave comments, you need to log in
Replace $this->model
with 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.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question