Jump to content
Fórum Script Brasil
  • 0

Script Ajax não funciona no Firefox


felps

Question

Eu fiz um código simples, só pra começar a aprender ajax, só que não funciona no firefox. Alguém poderia me ajudar com isso?

Index.html

<html>
    <head>
        <script type="text/JavaScript">
            var request = null;
            function createRequest(){
                try{
                    request = new XMLHttpRequest();
                }catch(trymicrosoft){
                    try{
                        request = new ActiveXObject("Msxml2.XMLHTTP");
                    }catch(othermicrosoft){
                        try{
                            request = new ActiveXObject("Microsoft.XMLHTTP");
                        }catch(failed){
                            request = null;
                        }
                    }
                }
                
                if(request == null)
                    alert("Erro");
            }    

            function getWord(){                
                createRequest();
                var url = "script.php";                
                request.open("GET", url, true);                    
                request.onreadystatechange = updatePage;                
                request.send(null);                
            }
            
            function updatePage(){            
                if(request.readyState == 4){                
                    if(request.status == 200){                        
                        var tex = request.responseText;    
                        document.getElementById("texto").value = tex;
                    }
                }
            }
            </script>
    </head>
    <body>
        <form>
            <input type="text" name="texto" id="texto" />
            <button onclick="getWord();">Enviar</button>
        </form>
    </body>
</html>
script.php
<?php
    
    echo "Palavra";

?>

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
Eu fiz um código simples, só pra começar a aprender ajax, só que não funciona no firefox. Alguém poderia me ajudar com isso?

Index.html

<html>
    <head>
        &lt;script type="text/JavaScript">
            var request = null;
            function createRequest(){
                try{
                    request = new XMLHttpRequest();
                }catch(trymicrosoft){
                    try{
                        request = new ActiveXObject("Msxml2.XMLHTTP");
                    }catch(othermicrosoft){
                        try{
                            request = new ActiveXObject("Microsoft.XMLHTTP");
                        }catch(failed){
                            request = null;
                        }
                    }
                }
                
                if(request == null)
                    alert("Erro");
            }    

            function getWord(){                
                createRequest();
                var url = "script.php";                
                request.open("GET", url, true);                    
                request.onreadystatechange = updatePage;                
                request.send(null);                
            }
            
            function updatePage(){            
                if(request.readyState == 4){                
                    if(request.status == 200){                        
                        var tex = request.responseText;    
                        document.getElementById("texto").value = tex;
                    }
                }
            }
            </script>
    </head>
    <body>
        <form>
            <input type="text" name="texto" id="texto" />
            <button onclick="getWord();">Enviar</button>
        </form>
    </body>
</html>
script.php
<?php
    
    echo "Palavra";

?>

Olá,

Coloquei um return false, <button onclick="getWord(); return false;">Enviar</button>, assim não atualiza a URL,

para mim funcionou assim..

Link to comment
Share on other sites

  • 0
Mas por que será que acontece isso?

A tag button no firefox é interpretada como um input type='submit'. Logo, ele envia o formulário. Como o seu form não tem um action definido, o firefox envia o formulário para a própria página, dando a impressão que é um refresh.

Use input type='button' ao invés de button e seu problema estará resolvido, sem a necessidade do return false.

Edited by fiote
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...