weixin_33698823 2017-12-24 18:19 采纳率: 0%
浏览 27

如何使这项工作?

I´m trying to learn ajax, sitting here sinse 3 hours and trying to understand what I need to make it run.

I´m using it on Scriptly with Xampp.

This is my code:

<head>
    <title>Titel</title>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Ajax Example!</title>

    <script src="js\jquery/jquery.js"></script>

    <script>
        $(document).ready(function(){
            $("#btnSubmit").click(function(){
            //alert("hello");
                $.ajax({
                    type: "POST",
                    url: "http://localhost/inde.php",
                    data: {
                        myName: "durgesh technoclass"
                    },
                    success: function(output){
                        alert(output);
                    }
                });
            });
        });
    </script>
</head>

<body>

<form>
    <button id="btnSubmit">Click me !</button>
</form>

I even watched a tutorial on Youtube and copied all the code from there. The rating of the tutorial is good, the code seems to work for him, but why doesn´t it work for me ?! I don´t understand that.

The Jquery works, but ajax doesn´t. The Script just reloads the page and displays nothing.

Please help.

Edit:

May it be that it doesn´t work, because xampp doesn´t run an actual server ?

On request, here´s the code of the "inde.php":

<?php
    echo "Welcome from Server";
?>

Edit 2: Solution: I messed up the path of the inde.php. Just fixed it.

It works but there´s a mistake in the code. It had been fixed by Tanvir Ahmad Sohan. You can find his fix in his answer down there.

  • 写回答

1条回答 默认 最新

  • ??yy 2017-12-24 18:48
    关注

    Try this...

    <script>
            $(document).ready(function(){
                $("#btnSubmit").click(function(event){
                event.preventDefault();
                //alert("hello");
                    $.ajax({
                        type: "POST",
                        url: "http://localhost/inde.php",
                        data: {
                            myName: "durgesh technoclass"
                        },
                        success: function(output){
                            alert(output);
                        }
                    });
                });
            });
        </script>

    As your button is inside a form, when you click the button, form is being submitted to the same page, so it just reloads. To prevent this from happening use preventDefault() to prevent the default behaviour.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗