V
V
Vitaly Pitalenko2021-09-29 18:48:38
AJAX
Vitaly Pitalenko, 2021-09-29 18:48:38

When saving JSON via AJAX, why is only a certain number of files saved?

When trying to save json files, for some reason it saves only 112 files out of 122. What could be the problem?

JS

$.ajax({
                            url: ajaxurl,
                            method: 'POST',
                            dataType: 'html',
                            data: data_agency,
                            async: false,
                            cache: false,
                            success: function (r) {
                                //получаем ответ от php скрипта
                                let result = JSON.parse(r);

                                //сохраняем полученные ID
                                data_json.agency[i].id_post = parseInt(result.id_post);
                                data_json.agency[i].user_id = parseInt(result.user_id);

                                //выводим информацию
                                $('.console').prepend('<p> > ======================================</p>');
                                $('.console').prepend('<p> > Создано агентство с ID ' + result.id_post + '</p>');
                                $('.console').prepend('<p> > Создано пользователь с ID ' + result.user_id + '</p>');

                                let data_save = data_json.agency[i];
                                let data_name = result.id_post + '.json';

                                //сохраняем полученную информацию в файл
                                $.post(ajaxurl, {action: 'nv_save_agency_json', data: {"agency": data_save}, name: data_name}, function (r){}, 'json');
                            }
                        });


PHP

add_action('wp_ajax_nv_save_agency_json', function () {
    $fileName = $_POST['name'];
    $myFile = NV_PARSER_DIR.'json/agency/'.$fileName;
    $fh = fopen($myFile, 'w') or die('can\'t open file');
    $stringData = json_encode($_POST['data']);
    fwrite($fh, $stringData);
    fclose($fh);
    wp_die();
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Pitalenko, 2021-09-30
@Rikisan

As it turned out, the volume of the post request was most likely exceeded. Rewrote for sending a string and not data and everything is ok.

N
Nadim Zakirov, 2021-09-30
@zkrvndm

Probably on the server side there is a plugin that gives results in parts and page by page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question