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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵