user2296604
user2296604

Reputation: 75

ajax "post" request failed

I am using ajax for the first time and passing data to another file using ajax request. The request goes through if I pass it using get which is by default but the moment I change it to post it does not work.

$.ajax({
            type:'POST',
            url:'pageAjax2.php',
            data:'name='+name,
            success: function(data){
                $('#content').html(data);
            }
      })

If I remove the type:'POST'; everything works but if have it in the code nothing works . Can someone please help me with this.

Upvotes: 1

Views: 80

Answers (2)

user2296604
user2296604

Reputation: 75

thank you guys i just checked and pageAjax2.php had been set to get instead of using post so i just changed it to post and everything works now thank you

Upvotes: 0

Telmo Dias
Telmo Dias

Reputation: 4168

All good here. What version of jQuery are you using ?

I'll post my code :

File jq.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script>

    $(function(){
        var name = 'Telmo Dias';

        $.ajax({
            type:'POST',
            url:'pageAjax2.php',
            data:'name='+name,
            success: function(data){
                $('#content').html(data);
            }
        });
    });
    </script>
</head>
<body>
    <div id="content"></div>
</body>
</html>

File pageAjax2.php :

<?php echo "Hello ".$_POST['name'];?>

Result:

Result

Upvotes: 1

Related Questions