【问题标题】:.NET Jquery Script Error.NET Jquery 脚本错误
【发布时间】:2018-02-12 14:38:39
【问题描述】:

尝试执行 Jquery 功能,一切似乎都设置正常,但我在浏览器控制台日志中收到此错误

SCRIPT5009: 'jQuery' 未定义 bootstrap.js (20,1)

HTML1300:发生导航。 1 (1,1) 2 CSS3121:媒体查询 -ms-viewport 已被弃用。

SCRIPT5009: SCRIPT5009: 'jQuery' 未定义 bootstrap.js (21,1)

SCRIPT5009: SCRIPT5009: '$' 未定义 SendEmail.js (1,1)

这是我的视图脚本/样式:

@model Linkofy.Models.EmailAccount

@{
ViewBag.Title = "SendMail";
}
@section Styles {
<link href="@Url.Content("~/Styles/Edit.css")" rel="stylesheet" type="text/css" />
}
@section Scripts {
<script src="@Url.Content("~/Javascript/SendEmail.js")"></script>      
}

我的功能(SendEmail.js):

$("#templateName").on("change", function () {

var tempID = $(this).find('option:selected').val();
var url = "/Identifiers/TemplateData/" + tempID;
console.log()

$.ajax({
    type: "GET",
    dataType: "json",
    contentType: "application/json",
    url: url, // Variabel
    showLoader: true,
    success: test // Function
});

});
function test(data) {
data = $.parseJSON(data);
$('#subject').val(data.subject);
$('#body').val(data.body);
}

捆绑包:

    // For more information on bundling, visit https://go.microsoft.com/fwlink/?LinkId=301862
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-"));

        bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                  "~/Scripts/bootstrap.js",
                  "~/Scripts/respond.js"));

        bundles.Add(new StyleBundle("~/Content/css").Include(
                  "~/Content/bootstrap.css",
                  "~/Content/site.css"));
    }
}
}

如果不填充此文件上的文本就无法发布,所以这是我的整个布局文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - linkofy</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")

</head>
<body>
<div class="navbar" style="background-color: LightSteelBlue;">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Linkofy", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Domains", "Index", "Identifiers")</li>
                <li>@Html.ActionLink("Clients", "Index", "Clients")</li>
                <li>@Html.ActionLink("Links", "Index", "Links")</li>
                <li>@Html.ActionLink("Status", "Index", "Status")</li>
            </ul>
            @Html.Partial("_LoginPartial")
        </div>
    </div>
</div>
<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
        <p>&copy; @DateTime.Now.Year - Orb Online - Liam Cook</p>
    </footer>
</div>

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
    @RenderSection("Styles", required: false)
</body>
</html>

【问题讨论】:

  • 你能展示你的layout.cshtml吗?
  • @DanielA.White 添加了它:)
  • 显示您的浏览器控制台。
  • @UbiquitousDevelopers 你的意思是从 fn+f12 窗口吗?我把所有东西都放在了最上面,还是你的意思是别的?
  • @Lucie - 是的,fn + f12 并显示控制台

标签: c# jquery .net asp.net-mvc


【解决方案1】:

该错误只能由以下三种情况之一引起:

  1. 您的 JavaScript 文件未正确加载到您的页面中

  2. 你有一个拙劣的 jQuery 版本。这可能发生,因为 有人编辑了核心文件,或者插件可能覆盖了 $ 变量。

  3. 您在页面完全加载之前运行了 JavaScript,并且作为 这样,在 jQuery 完全加载之前。

您应该检查 Firebug 网络面板以查看文件是否真正正确加载。如果没有,它将以红色突出显示,并在其旁边显示“404”。如果文件加载正确,这意味着问题是 2。

确保所有 jQuery javascript 代码都在代码块中运行,例如:

$(document).ready(function () {
  //your code here
});

这将确保您的代码在 jQuery 初始化之后被加载。

最后要检查的是确保在加载 jQuery 之前没有加载任何插件。插件扩展了“$”对象,所以如果你在加载 jQuery 核心之前加载插件,那么你会得到你描述的错误。

注意:如果您正在加载不需要 jQuery 运行的代码,则不需要将其放在 jQuery 就绪处理程序中。该代码可以使用

分隔

document.readyState

确保在您的布局中的标题标签脚本应该是这样的顺序:

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多