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 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时
  • ¥15 脚本求解,源于数据库脚本变量筛选。