April 4, 2013

$.ajax doesn't response

Nurhidayat Fembrianto’s Questions:

i have some code here that doesn’t work at all:

    <script type="text/javascript">
    function get_all(){
        var mutation = document.getElementById("mutation");
        $.ajax({
            url:"http://localhost/mandala/test/xx",
            success: function(msg){
                mutation.innerHTML = msg;
            },
            error: function(x,status,error){
                alert(status+":"+error);
            }
        });

    }
</script>

<html>
    <body>
        <input type="button" onclick="get_all()" value="Click">
        <div id="mutation">

        </div>
    </body>
</html>

I don’t think so if there is any problem with my url neither the code. But i hope some body can help me out with this problem.

Your HTML Structure in invalid. At the time of Script execution, it will not find the element. So use this instead.

function get_all(){
    $.ajax({
        url:"http://localhost/mandala/test/xx",
        success: function(msg){
            $("#mutation").html(msg); //<!-- jQuery Selector
        },
        error: function(x,status,error){
            alert(status+":"+error);
        }
    });

}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!