A
A
Alexander Ampleev2018-05-25 12:12:05
Yii
Alexander Ampleev, 2018-05-25 12:12:05

Why does POST not send data to yii2 server?

Here is the form sent by Ajax:

<form id="myForm">
                    <input id="idmodel" type="hidden" name="idmodel" value="28254">
                    <input id="myFiles" name="myFiles" type="file" size="1"/>
                </form>


here is the ajax request:

var urlPrefix = 'http://site.com/';
    var url = urlPrefix + 'dev/my-function';

    $.ajax({
        type: 'post',
        url: url,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        xhr: function () {
            var xhr = $.ajaxSettings.xhr(); // получаем объект XMLHttpRequest
            xhr.upload.addEventListener('progress', function (evt) { // добавляем обработчик события progress (onprogress)
                if (evt.lengthComputable) { // если известно количество байт
                    // высчитываем процент загруженного
                    var percentComplete = Math.ceil(evt.loaded / evt.total * 100);
                    // устанавливаем значение в атрибут value тега <progress>
                    // и это же значение альтернативным текстом для браузеров, не поддерживающих <progress>
                    // progressBar.val(percentComplete).text('Загружено ' + percentComplete + '%');

                    console.log('percentComplete: ', percentComplete);
                }
            }, false);
            return xhr;
        },
        success: function (data) {
            console.log(data);
        }

    });


code in controller:

public function actionMyFunction()
    {
        $key = Yii::$app->request->post();
        return json_encode($_POST);
        $file = $_FILES;
        $id = $key['idmodel'];
        $model = $this->findModel($id);
        $model->myFiles = UploadedFile::getInstanceByName('myFiles');
        return json_encode($model->myUpload());
        if ($model->myUpload()) {
            return ($model->save(false));
        }
    }


Returns correct data when testing locally: When testing in production, it returns an empty array for some reason{"idmodel":"28254"}

[]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2018-05-25
@Ampleev

Well, it is obvious that in production the file does not fit into the post-request. Need to increase

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question