L
L
lzy2019-08-29 20:24:25
Yii
lzy, 2019-08-29 20:24:25

How to get article content via AJAX?

Hello! Articles are displayed cyclically on the site, you need to create an AJAX request that would display more detailed content in popUp. Please help me figure this out. How to create a request in general and display it back? In the current state, I receive an object - how to parse it, if this is correct at all?

$.ajax({
            url: '/category/data',
            data: {productId:productId},
            type: 'GET',
            success: function(res){
                alert(res)

            },
            error: function(){
                alert('Error!');
            }
        });


public function actionData($productId){
        if (Yii::$app->request->isAjax) {

            $res = Product::find()->where(['id'=> $productId])->one();
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return ['res'=> $res];
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-08-29
@lzy

Just replace

success: function(res){
                alert(res)

            },

on the
success: function(res){
                console.log(res)

            },

and before calling open developer console (F12)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question