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 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。