Developer
Developer

Reputation: 2706

Ajax Data response data not alert

I want to alert some data using of ajax callback but not alerting.

<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js?ver=4.0'></script>
<script>
var mainObject = {
        myFunction: function() {
        $.ajax('ajax.php',function(data){
            alert(data);
            //$('#test123').html(data);
        });
    }
};
</script>
<div id="test123"></div>
<a href="javascript:mainObject.myFunction();">Click here</a>

ajax.php

<?php echo "Hello World!!!"; ?>

I am getting the response Hello World!!! but not alerting. Please check above code.

Upvotes: 0

Views: 742

Answers (4)

Chetan Gawai
Chetan Gawai

Reputation: 2401

Try this

<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js?ver=4.0'></script>
<script>
var mainObject = {
        myFunction: function() {
        $.ajax({
            url : 'ajax.php',
            success:function(data){
            alert(data);
           }
        });
            //$('#test123').html(data);
        }
    }

</script>
<div id="test123"></div>
<a href="javascript:mainObject.myFunction();">Click here</a>

Upvotes: 0

soundhiraraj
soundhiraraj

Reputation: 323

Try This,if you get success it will alert your data else it alert error message

$.ajax('ajax.php',success:function(data){
      alert(data);
    }
error:function()
{
alert("error");
}
});

Upvotes: 0

Chinmay235
Chinmay235

Reputation: 3414

Use $.post instead of $.ajax

code:

var mainObject = {
        myFunction: function() {
        $.post('ajax.php',function(data){
            alert(data);
            //$('#test123').html(data);
        });
    }
};

Upvotes: 0

Gowri
Gowri

Reputation: 1856

You have to use success function in ajax

Try Like this

 $.ajax('ajax.php',{
      success : function(data){        // Response from the php file
                alert(data);
            }
 });

Upvotes: 1

Related Questions