L
L
Lev K2016-05-06 16:14:00
JavaScript
Lev K, 2016-05-06 16:14:00

Why is the data from the server not in json format?

Hello.
I send the modal form data to the server like this:

$('body').on('submit', '.modal-form', function (e) {
        e.preventDefault();
        var form = $(this);
        $.ajax({
            url: '" . Url::to(['submitmodal']) . "',
            type: 'POST',
            data: form.serialize(),
            success: function (result) {
                setTimeout(function() { $('#my-modal').modal('hide');}, 500);
                var id = result.id;
                var type = result.type;
                $('#dropDownList-types').append('<option value='+id+'>'+type+'</option>');
            },
            error: function () {
                alert('error in ajax modal submit');
            }
        });
    });

Controller
public function actionSubmitmodal() 
    {
        $model = new Typecostauto();
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return ['id' => $model->id, 'type' => $model->Type];
            }
            else 
                return ['id' => 'fail'];
        }
        else 
        return $this->renderPartial('modal', [
            'model' => $model,
        ]);
    }

In the controller, I indicated that I want to send JSON data, and if dataType: 'json' then nothing works.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2016-05-06
@webinar

1. Why renderPartial and not renderAjax?
2. To put your render into json, you need to do this:

Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$myvar = $this->renderAjax('modal', [
            'model' => $model,
        ]);
return yii\helpers\Json::encode($myvar);

and watch debug for errors if it doesn't work

K
Kirill Arutyunov, 2016-05-06
@arutyunov

Inside $.ajax specifydataType: 'json'

R
Roman, 2015-06-15
@valo1114

What version of OpenCV do you have installed?
There is no such method in OpenCV 3.0 and in version 2.x, too, in my opinion, there was no such method. Try as described here .
It's strange that your module is called cv and not cv2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question