Json Encode Problem

Hi Everyone,

When i clicked delete button this js function has called.


 function RemoveFavorite(obj)

                            {

                                if (confirm('Ürün Favorilerizden Çıkarılacak. Emin Misiniz?')) {

                                    $.ajax({

                                        type: 'POST',

                                        data: {userFavoriteId: obj},

                                        url: '/testdrive/index.php/shopping/RemoveFavorite',

                                        success: function(data) {

                                            $('#userFavDiv').html(data);

                                        },

                                        error: function(data)

                                        {

                                            alert('Onaylanmadı!!');

                                        }

                                    });

                                }

                            }

.

in shopping/RemoveFavorite method like that:




  public function actionRemoveFavorite() {

        $Id = $_POST['userFavoriteId'];

        $model = UserFavorite::model()->findbyPk($Id);

        $model->delete();

        echo CJSON::encode($this->renderPartial('/shopping/userFavorite'),true,false);

    }



.

json::encode function return me like response:


<meta charset="utf-8">

<section class="other-books subsection divider-top-thick">

    <h3>Favoriler</h3>

    <table class="table table-striped table-header">

        <tr style='background-color: #999999'>

        </tr>

        <tbody>

                            <tr>

                    <td>

                        <a  href="/testdrive/index.php/book/bookDetail?bookId=1">URSULE MIROUET</a>

                    </td>

                    <td>

                        <a class="cart-item-remove-option icon-trash" title='Favorilerden Çıkar' href="#" onclick="RemoveFavorite('59');">

                        </a>

                    </td>

                </tr>

                    </tbody>

    </table>

</section>

<script>

                            function RemoveFavorite(obj)

                            {

                                if (confirm('Ürün Favorilerizden Çıkarılacak. Emin Misiniz?')) {

                                    $.ajax({

                                        type: 'POST',

                                        data: {userFavoriteId: obj},

                                        url: '/testdrive/index.php/shopping/RemoveFavorite',

                                        success: function(data) {

                                            $('#userFavDiv').html(data);

                                        },

                                        error: function(data)

                                        {

                                            alert('Onaylanmadı!!');

                                        }

                                    });

                                }

                            }

</script>null

.

Did you see null string which is end of the respond. Dou you know why respond have null string?

Hi aalicagan,

I think you don’t need to JSON encode your response. What you are expecting in your ‘success’ function is a plain html.

Hi softark;

my aim is: when i clicked delete button for table row, refresh only table data. to make it i delete record on db and return new html with json.

I think this is enough:




  public function actionRemoveFavorite() {

        $Id = $_POST['userFavoriteId'];

        $model = UserFavorite::model()->findbyPk($Id);

        $model->delete();

        $this->renderPartial('/shopping/userFavorite'));

    }



We have to return json when we want to pass organized data … we would json-encode some PHP nested array to javascript objects.

But you just need a plain html here.

This is a common issue with json_encode

it fails to encode text with non-utf8 characters

like you have

>> [color=#008800][size=2]Ürün Favorilerizden Çıkarılacak. Emin Misiniz?[/size][/color]

you need to ensure utf8 encoding of this text

but softark is right - it seems that you dont need to json_encode a piece of HTML code, just return it and compose via innerHtml to needed place on your frontpage.

But remember AJAX also require correct UFT8 encoding.