weixin_33749242 2017-02-21 01:49 采纳率: 0%
浏览 105

Web开发。 阿贾克斯

Can someone please guide me through where am I going wrong in this piece of code below

index.php :-

<script type="text/javascript">
function fun(){
    alert("H");
    $.ajax({
    type: "POST",
    url: 'test.php',
    data: {name: 'Wayne', age: 27},
    success: function(data){
        alert(data);
    },
    error: function(error){
        alert(error);
    }
});
    alert("HE");
    }
    </script>
    <input type="button" value="submit" onclick="fun()" /> 

test.php :-

<?php
    $name = $_POST['name'];
    $age = $_POST['age'];
    echo $name.$age;
?>

I'm not getting any output nor any error.

  • 写回答

1条回答 默认 最新

  • weixin_33691700 2017-02-21 01:57
    关注

    The problem is in the javascript code, specifically in how you're calling the function. Try this instead:

    // javascript
    document.getElementById('do-fun').addEventListener('click',function(){
        fun();
    });
    
    // html
    <input type="button" value="submit" id="do-fun" /> 
    
    评论

报告相同问题?