problem ajax with nginx

doudootiana nginx-forum at forum.nginx.org
Wed Sep 9 09:56:39 UTC 2020


Hy Ladies and gentlemen :)

I have migrated my project from Apache2 and php7 to nginx with php-php,
version 7 too.

With Apache2, everything worked.
With Nginx, only ajax fails.

With Ajax, i try to execute a php script.

Trying to determine where is the problem, i have :

-tested php code(echo and phpinfo), directly in html: it works.
-tested a javascript which change the appearances of my page : it works.
-tested in command line my script php : it works. ( The php script launch a
sh script to reboot my Raspberry.)

In the javascript where is my ajax, i used success error and complete to
return the result.
and the function of success and complete are executed. but the card doesn't
reboot.

I have no error log from nginx and php-fpm :(

I've been looking into the problem for several days, which must be a big
mistake on my part.

here the fonc of nginx :


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request"
'
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /diagbox/web;
            index  intro.html;
        }

        error_page  550              /550.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
        

        # pass the PHP scripts to FastCGI server listening on
127.0.0.1:9000
        #
        location ~ \.php$ {
		fastcgi_pass unix:/run/php-fpm.sock;
		fastcgi_param SCRIPT_FILENAME /diagbox/web/$fastcgi_script_name;
		include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based
configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

here the js script :

//Constantes fonction reboot
const scripts_reboot = '../scripts/reset_reboot/reboot.php'
const mess_reboot = "La DiagBox redémarrera après avoir cliquer sur 'ok'. Tu
pourras te reconnecter sur l'interface web d'ici 30 secondes environ :)"
const err_reboot = "Une erreur a empeché le redémarrage de la carte."

//Constantes fonction popup test debit
const php_download = "../scripts/test_debit/iperf_download_bouygues.php"
const php_upload = "../scripts/test_debit/iperf_upload_bouygues.php"
const php_ping = "../scripts/test_debit/iperf_ping.php"

function reboot(param1, param2) // param1 = smartphone ou tablette . param2
= oui ou non
{
	if (param1 === 'tablette')
	{
		switch (param2)
		{
			case 'oui':
				alert(mess_reboot);
					$.ajax({
						url: scripts_reboot,
					});
				break;

			default:
				choix_menu('accueil', 'tablette'); // Fonction accessible grace a la
page html
				break;		
		}
	}

	else if (param1 === 'smartphone')
	{
		switch (param2)
			{
				case 'oui':
					alert(mess_reboot);
						$.ajax({
							url: scripts_reboot,
						});
					break;

				default:
					choix_menu('accueil', 'smartphone'); // Fonction accessible grace a la
page html
					break;		
			}
	}

	else
	{
		alert(err_reboot);
	}

} //fin script reboot

Here the html code, the part which uses the js code :

<div id="reboot">
				<h2 class="h2_reboot"> confirmer le redémarrage de la Diagbox ? </h2>
				<input id="reboot_yes_tablette" type="button" value="Oui"
class="tablette_bouton" onclick="reboot('tablette', 'oui');" /> <!-- Bouton
pour oui -->
				<input id="reboot_no_tablette" type="button" value="Non"
class="tablette_bouton" onclick="reboot('tablette', 'non');" /> <!-- Bouton
pour non -->
</div>

and here, the php code :

<?php
define('script_reboot', 'sh /diagbox/scripts/reset_reboot/reboot.sh');
echo exec(script_reboot);
?>


Thanks by advance :)

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,289351,289351#msg-289351



More information about the nginx mailing list