2013-07-16 67 views
9

我想要一個非常簡單的模態對話框運行。所以下面的教程我結束了這段代碼:0x800a1391 - JavaScript運行時錯誤:'jQuery'未定義 - MVC 4

BundleConfig:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-1.8.2.min.js")); 

bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
        "~/Scripts/jquery-ui-1.8.24.js")); 

_layout:

<head> 
    <meta charset="utf-8" /> 
    <title>@ViewBag.Title - My ASP.NET MVC Application</title> 
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /> 
    <meta name="viewport" content="width=device-width" /> 
    @Styles.Render("~/Content/css") 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/bundles/jqueryui") 

</head> 
<body> 
    <div id="body"> 
     @RenderSection("featured", required: false) 
     <section class="content-wrapper main-content clear-fix"> 
      @RenderBody() 
     </section> 
    </div> 
    <footer> 
     <div class="content-wrapper"> 
      <div class="float-left"> 
       <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p> 
      </div> 
     </div> 
    </footer> 


    @RenderSection("scripts", required: false) 
</body> 

和索引:

@section featured { 
<section class="featured"> 
    <div class="content-wrapper"> 
     <hgroup class="title"> 
      <h1>@ViewBag.Title.</h1> 
      <h2>@ViewBag.Message</h2> 
     </hgroup> 
     <script type="text/javascript"> 
      $("#dialog-form").dialog({ 
       autoOpen: false, 
       height: 300, 
       width: 350, 
       modal: true, 
       buttons: { 

        Cancel: function() { 
         $(this).dialog("close"); 
        } 
       }, 
       close: function() { 
        allFields.val("").removeClass("ui-state-error"); 
       } 
      }); 

      $("#create-user") 
       .button() 
       .click(function() { 
        $("#dialog-form").dialog("open"); 
       }); 

     </script> 

     <div style="float: left; width: 250px;"> 
      <button id="create-user">Create new user</button> 
     </div> 

    </div> 
</section> 
} 

然而,當我運行它,我結束與0x800a1391 - JavaScript運行時錯誤:'jQuery'是未定義的,在jquery-ui庫中。如果我只是簡單地把代碼放在一個html頁面中,它就能按預期工作。所以問題來自MVC項目。我在Windows 8上使用visual studio 2012.任何想法?

+0

你能向我們展示在運行後如何渲染 –

+2

在瀏覽器中,查看生成的實際腳本元素。您可能會發現jQuery腳本的路徑以某種方式關閉。 –

+0

只有 出現在頭部。 – Wosh

回答

9

默認情況下,MVC捆綁器會忽略文件名中包含.min的文件。

使用未縮小版本的jQuery來修復問題(或者只是重命名文件) - 部署時,捆綁器將無論如何都會縮小jQuery文件。


更新

您可以通過在RegisterBundles方法清除IgnoreList改變這種行爲(但我建議使用默認設置貼合,只需重命名文件):

// Clear all items from the ignore list to allow minified CSS and JavaScript 
// files in debug mode 
bundles.IgnoreList.Clear();  

// Add back the default ignore list rules sans the ones which affect minified 
// files and debug mode 
bundles.IgnoreList.Ignore("*.intellisense.js"); 
bundles.IgnoreList.Ignore("*-vsdoc.js"); 
bundles.IgnoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled); 

見更多在Telerik docs

+0

如果我這樣做,我結束了'異常被引發在5288行,第6列在http:// localhost:53886/Scripts/jquery-1.8.2.js 0x800a139e - JavaScript運行時錯誤:SyntaxError' – Wosh

+0

你有沒有改變BundleConfig文件中的路徑,並重建項目? –

+0

是的,現在jquery出現在head元素中 – Wosh

10

發生此錯誤時遇到的問題是由於以下事實不在文檔的<head>中,而是在文檔末尾的</body>標記之前。

@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/bundles/jqueryui") 
@Scripts.Render("~/bundles/bootstrap") 

一旦我將它們移動到我的.cshtml文件的<head>,一切工作正常。

+0

+1,因爲這幫助了我。我必須刪除Layout.cshtml文件中的@ Script.Register,並將其包含在我正在處理的cshtml文件中。 –

+0

MVC項目的默認佈局是將scripts.render部分放在主體底部附近喜歡這個。 – redwards510

2

我在這個問題上掙扎了大約一個小時,幾乎所有的解決方案都歸結爲您稱之爲腳本的順序和方法。我發現打包商造成的問題(對我來說,它是UI打包機)

這是我的工作順序。

<script src="/Scripts/modernizr-2.5.3.js"></script> 
    <script src="/Scripts/jquery-1.7.1.js"></script> 
    <script src="/bundles/jquery-ui"></script> 
    <script src="/Scripts/jquery.unobtrusive-ajax.js"></script> 
    <script src="/Scripts/jquery.validate.js"></script> 
    <script src="/Scripts/jquery.validate.unobtrusive.js"></script> 
5

只需使用@section腳本在你的腳本,像這樣

@section scripts 
    { 
    <script> 
    $(function() { 



     }); 

    }); 

    </script> 
} 
+1

這似乎工作和更優雅,當然,在同一個文件中有兩個不同的東西的潔具! – JosephDoggie

1

IE有其他語法檢查在javascript,比折,Chrome和Firefox。 當我的MVC應用程序中打開BundleOptimization時,我的代碼中出現同樣的錯誤。 我用AngularJS爲框架,一條線搞砸了整束,並做了連鎖效應:

$http.post('/controller/action', { dto }) 

卸下大括號{} - 解決了我的「缺少jQuery的問題」。但是,我第一次在IE中調試發現(在調試模式下按F8):enter image description here JQuery錯誤消息是誤導性的,但語法錯誤指的是正確的行,所以我可以修復錯誤。

相關問題