JQuery AJAX - Returned HTML Zero

I work with jQuery AJAX and stumbled upon a weirdness (or rather a lack of understanding on my part :). Here is the code for my aspx homepage:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="jQuery_Test.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnGo').click(function() {
                $.ajax({
                        type: "GET",
                        url: 'GetValues.aspx',
                        contentType: 'text/html',
                        success: function (html) { $("#Yo").append(html); }
                });
            })
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p id="Yo">Yo there!</p>
        <br />
        <a href="#" class="yo">Yo there!</a>
        <br />
        <a href="#">I'm in a DIV!</a>
        <br />
        <button id="btnGo">Go</button>
    </div>
    <br />
    <br />
    <a href="#">I'm not in a DIV!</a>
    </form>
</body>
</html>

and here is the code for the "GetValues.aspx" page:

using System;

namespace jQuery_Test
{
    public partial class GetValues : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/html";
            Response.Write("<a href='#'>Hello World</a>");
            Response.End();
        }
    }
}

All I do is when the Go button is clicked on the main page, I want the anchor tag to be placed next to the paragraph.

The code does work, however, the "Hello World" anchor appears for about 1 second and then disappears. Can someone point out what I'm doing wrong? I am using jQuery-1.3.2.mins.js which comes with VS 2010 Ultimate.

Greetings. Jac.

+3
source share
2 answers

make your function return false;

    $(document).ready(function () {
        $('#btnGo').click(function() {
            $.ajax({
                    type: "GET",
                    url: 'GetValues.aspx',
                    contentType: 'text/html',
                    success: function (html) { $("#Yo").append(html); }
            });

            return false;
        })
    });
+3

, ( ). false , , .

0

Source: https://habr.com/ru/post/1734939/


All Articles