weixin_33717298 2016-01-15 01:34 采纳率: 0%
浏览 20

ajax“发布”请求失败

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.

  • 写回答

2条回答 默认 最新

  • weixin_33743661 2016-01-15 01:48
    关注

    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

    评论

报告相同问题?