jquery - '$' 未定义。如何在空的 ASP.NET MVC 4 项目中使用 jQuery 2.0.1?

标签 jquery asp.net asp.net-mvc asp.net-mvc-4 razor

我已经看过了:'$' is undefined ,但这并没有解决我的问题;这就是为什么我打开另一个线程。

这就是我所做的...

我使用 Visual Studio 2012 创建了一个新解决方案,并添加了一个空的 ASP.NET MVC 4 项目。我添加了一个 Controller 类,定义了一个默认的 Index 操作方法,并让 Resharper 为其创建 View 。该项目具有以下结构:

root/
    Controllers/
        SampleController.cs
    Models/
    Views/
        Sample/
            Index.cshtml
        Shared/
            Layout.cshtml
    Scripts/
        jquery-2.0.1.js
        jquery-2-0.1.min.js
        jquery-2.0.1-min.map

jQuery 已通过 Nuget 添加;因此,我手动将 BundleConfig.cs 文件添加到 App_Start 文件夹中,并添加代码来定义 jQuery 脚本包(RegisterBundles 将由 global.asax 调用)。

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery")
        .Include("~/Scripts/jquery-2.0.*"));

}

我还定义了一个具有以下内容的布局页面...

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>@this.ViewBag.Title</title>
    </head>
    <body>
        <div>
            @this.RenderBody()
        </div>
        @this.RenderSection("Scripts", required: false)
    </body>
</html>

我的索引 View 具有以下内容...

@using System.Web.Optimization
@model dynamic

@{
    this.Layout = "~/Views/Shared/Layout.cshtml";
}

@section Scripts
{
    @Scripts.Render("~/bundles/jquery")

    <script type="text/javascript">
        $(document).ready(function() {

        });
    </script>
}

以下标记将呈现到输出流...

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
    </head>
    <body>
        <div>

        </div>

    <script src="/root/Scripts/jquery-2.0.1.js"></script>
    <script src="/root/Scripts/jquery-2.0.1.min.map"></script>

    <script type="text/javascript">
        $(document).ready(function() {

        });
    </script>

    </body>
</html>

当我调试应用程序时,它给我错误消息“$”未定义。我没有将应用程序托管在本地 IIS 中,而是尝试通过 Visual Studio 的 Web 开发服务器运行它,这给了我另一个提示:语法错误(min.map 文件中出现意外的标记“;”)。可能是 jQuery 未初始化的原因...但我应该如何解决这个问题?

最佳答案

实际问题与我的本地 IIS 有关,其中静态内容功能不可用。这会导致对 java 脚本和 css 文件的请求得到 OK 200 的响应,但内容长度为零。请参阅:https://serverfault.com/questions/115099/iis-content-length-0-for-css-javascript-and-images有关如何修复 IIS 安装的信息。

以下代码现在可以按预期工作。唯一改变的是脚本包定义......

BundleConfig.cs

namespace MvcApplication1
{
    using System.Web.Optimization;

    public class BundleConfig
    {
        public const string BundlesJquery = "~/bundles/jquery";

        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle(BundlesJquery)
                .Include("~/Scripts/jquery-{version}.js"));
        }
    }
}

索引.cshtml

@using System.Web.Optimization
@using MvcApplication1
@model dynamic

@{
    this.ViewBag.Title = "title";
    this.Layout = "~/Views/Shared/Layout.cshtml";
}

@section Scripts
{
    @Scripts.Render(BundleConfig.BundlesJquery)
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Hello World.");
        })
    </script>    
}

布局.cshtml

@using System.Web.Optimization
@using MvcApplication1
<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>@this.ViewBag.Title</title>
    </head>
    <body>
        <div>
            @this.RenderBody()
        </div>
        @this.RenderSection("Scripts", false)
    </body>
</html>

关于jquery - '$' 未定义。如何在空的 ASP.NET MVC 4 项目中使用 jQuery 2.0.1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16862643/

相关文章:

javascript - 在我的 xml 调用中添加一个函数

jquery - 将当前 CSS 值保持在变量中而不更改

ASP.NET 身份和移动客户端

c# - asp.net mvc Controller 中的封装开关逻辑

我的 javascript/jquery 代码存在 JavaScript NaN 问题

jquery - 切换根据操作替换文本

c# - 自动完成扩展器在模式弹出扩展器内不起作用

javascript - 通过推送数组将数据添加到 highstocks

html - 如何解决 ASP.Net MVC 5 中的 glyphicons-halflings-regular.woff2 Err_Aborted 问题

javascript - C# MVC AJAX 请求