Ajax returns error after modifying htaccess

Asked

Viewed 237 times

3

I modified the .htaccess with a rule for the url to be passed as the GET parameter and I handle everything on index.php:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?params=$1 [QSA]

After this change, the ajax (post) below returns as error and does not complete my request:

$("#criar").click(function()
    {
        $.ajax
       ({
            type : 'post',
            url : "http://www.xxxx.com.br/criar-pagina-ajax.php",
            data: 
            {
                nomepagina: $('#nome-pagina').val(),
                idcategoria: $('#id-categoria').val(),
                idpaginacurtir: $('#id-pagina-curtir').val(),
                youtube: getYoutubeId($('#youtube').val()) 
            },
            dataType : "json",
            beforeSend : function()
            {
                $("#mensagem").html('Verificando...');
            },
            success : function(data)
            {
                $("#mensagem").html(data.mensagem);
                if (data.cod == 1)
                {
                    location.reload();
                }
            }
        })
    });

I am not able to solve this problem, I believe the URL is being rewritten with the rule of .htaccess and the POST is not recognized in criar-pagina-ajax.php.

1 answer

2


Yes, the problem in this case is the URL. I see how solution change your URL:

Of: http://www.xxxx.com.br/criar-pagina-ajax.php

To: http://www.xxxx.com.br/criar-pagina

So when you split your URL you’re picking by the variable $_GET['params'] in the index.php you will see that the request is to create the page, and you can call the function that is written in the script criar-pagina-ajax.php

EDIT
A basic example in index.php:

$requisicao = explode("/", $_GET['params']);
$parametro = $requisicao[count($requisicao) - 1]; // $parametro = "criar-pagina"
if ($parametro == "criar-pagina") {
    // Faça alguma coisa
}
  • But the RewriteCond %{REQUEST_FILENAME} !-f should not prioritise criar-pagina-ajax.php, whereas it exists?

  • Well, what I can tell you is that the rules RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME} !-f verify if the request is a directory or a common file ( txt, css, js, etc. ( The .php is not influenced, because I believe it is considered as something other than a file ) respectively. If he goes through these 2 conditions, he goes to the last rule, where the request is sent to the index.php within the variable $_GET['params']

  • Hm, I figured he’d get php.

Browser other questions tagged

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