jQuery引起错误:单击没有代码的内容时未定义



我是新手的jQuery,正在乱七八糟,并阅读了一些可爱的json,更新了所有内容。单击它时,它可以进行下载,但也说"错误:未定义",这似乎是因为填充JSON结果的代码尚未完成,但我不确定如何避免。

<%@ Page Title="TEST Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="RSChanges.WebForm1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script>
        function getg1()
        {
            showData = $('#group1');
            $.ajax({
                url: '/url1',
                type: 'GET',
                success: function (data)
                {
                    var content="";
                    var i;
                    for (i = 0; i < data.HDData.length; i++)
                    {
                            content += '<li>' + data.number+ '</li>';
                    }
                    showData.html(content);
                },
                error: function (xx, textStatus, errorThrown)
                {
                    alert("error: " + xx.responseText);
                }
                });
        }
        $(document).ready(function ()
        {
            $('body').css('background-color', '#095BB5');
            $('body').css('color', 'white');
            $('a').css({ 'color': '#E0FFFF', 'textDecoration': 'underline' });
            $('#g1').click(function () { $('#group1').toggle(); });
            $('#group1').toggle();
            getg1();
        });
    </script>
       <div> Link to the current <a id="currentform" href="form.xls">download form</a><br /><br /></div>
    Current Active group1 (click <a id="g1">here</a> to show/hide)<br />
    <div class="group1" id="group1">(Loading please wait...)</div>
</asp:Content>

所以,这是代码,我煮了一些JSON更新的方式,并更改了保护无辜的事物的名称。

但是,它仍然可以做到这一点,启动页面,然后..在您单击其说明错误的表单时:在弹出框中未定义的那一刻。如果我单击组并将它们展开(仅此处显示的1个),然后等到下载的所有内容,似乎不会发生。我如何没有错误的框弹出?

Masterform-

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="RSChanges.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %></title>
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <style> 
        html, body{ height: 100%;}
    </style>
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>
        <div class="container body-content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"></asp:ContentPlaceHolder>
        </div>
    </form>
        <footer>
            <p>&copy; <%: DateTime.Now.Year %></p>
        </footer>
</body>
</html>

通过单击链接,您将启动新的HTTP请求。这可能会中断页面上当前运行的任何JS,包括AJAX请求。通常,单击超链接(或提交表单)会导致您的页面被销毁并加载新页面,这显然会杀死页面中运行的所有JS。

由于此请求恰好返回要下载的文件,因此显然这并没有发生。但是,它仍然可能会中断JS执行,以期从服务器中获取新的HTML页面。

如果您放置

target="_blank" 

<a标签中,它在另一个选项卡/窗口中打开URL,然后打开文件不应干扰当前页面,因为浏览器不再期望替换它。

从您的代码中,我可以看到您从已发送Ajax请求的URL中获取您的代码。尽管我建议您检查您所获得的响应。console.log(xx),因为显然XX没有键'ResponseText'的数组。我们是未定义的。

最新更新