Problems with ajax on the server

Asked

Viewed 103 times

0

I have an application in ZF2 on Ubuntu server. One of the controllers is triggered from a request ajax.

Locally, the application works normally. However, when going up to the server I started having problems: the request is sent as POST, but I can’t get the data from the controller, because it arrives as GET and with the empty data array.

Would anyone know the problem, since the same code works in local environment?

In the controller the get of data:

$data = $this->getRequest()->getPost()->toArray();
            $request = $this->request;
            $post = array_merge_recursive(
                    $request->getPost()->toArray(), $request->getFiles()->toArray()
            );
            $postData = array();
            $error = array();
            $errSuccess = array();

            //get Query String
            parse_str($post['data'], $postData);

In html I have the following code:

wizard.on("submit", function (wizard) {

            var formData = new FormData();
            formData.append('upload', $('input[type=file]')[0].files[0]);

            var ins = document.getElementById('gallery').files.length;
            for (var x = 0; x < ins; x++) {
                formData.append('gallery[]', $('input[type=file]')[1].files[x]);

            }

            formData.append('data', wizard.serialize());

            $.ajax({
                type: "POST",
                url: '/advertiser/add',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success: function (resp) {
                    if (resp.response == 1) {
                        $(".create-server-name").text($("#nome_razao").val());
                        wizard.submitSuccess();
                        wizard.hideButtons();
                        wizard.updateProgressBar(0);
                        $.each(wizard.cards, function (name, card) {
                            card.el.find("input").val(); // resets all inputs on a card to ""
                        });
                    } else if (resp.response == 2) {
                        var str = '';
                        for (obj in resp.result) {
                            str += "<li>" + resp.result[obj] + "</li>";
                        }
                        $(".create-server-name").text($("#nome_razao").val());
                        $('#errmsg').html(str);
                        console.log(str);
                        wizard.submitSuccess();
                        wizard.hideButtons();
                        wizard.updateProgressBar(0)
                        $.each(wizard.cards, function (name, card) {
                            card.el.find("input").val(); // resets all inputs on a card to ""
                        });
                    } else {
                        var str = '';

                        $('#errmsgerr').html(resp.result);
                        console.log(str);
                        wizard.submitError();
                        wizard.hideButtons();
                        wizard.updateProgressBar(0)
                    }
                },
                error: function () {
                    wizard.submitFailure();
                    wizard.hideButtons();
                },
            });

        });

php.ini was not changed after the installation. php version:

PHP 5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

This is . htaccess, which is the same for both environments:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
  • Fran, I see that you are having upload files. My suggestion is you use something ready. Have a very good file upload plugin, see if this can help. http://www.dropzonejs.com

  • The template itself that I use forces something similar to Dorpzone.

  • What version of PHP is in place and on the server? Please put the relevant php.ini settings.

  • php.ini was not changed after installing php

  • Check the directive: enable_post_data_reading php.ini from your server

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.