C# 0x800a1391 - JavaScript 运行时错误:'jQuery' 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15948763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
0x800a1391 - JavaScript runtime error: 'jQuery' is undefined
提问by Francis Rodgers
I have a ASP .Net MVC4 Web application. In it I have my usual html for the _Layout.cshtml, which in turn loads the default Home/Index. All works fine.
我有一个 ASP .Net MVC4 Web 应用程序。在其中我有我常用的 _Layout.cshtml html,它依次加载默认的 Home/Index。一切正常。
In my index I am also loading a partial view. This works fine too. No probs.
在我的索引中,我还加载了一个局部视图。这也很好用。没有问题。
I am using a the UI tools from the following site:
我正在使用来自以下站点的 UI 工具:
http://www.keenthemes.com/preview/index.php?theme=metronic
http://www.keenthemes.com/preview/index.php?theme=metronic
The problem is it seems to be primarily HTML4 and not designed for MVC out of the box so I am having to tweak it slightly to get it to work the way I want. (Nothing beyond anything very basic). For example, moving one part out to the index and using Renderbody() to load it so the actual html structure never changes. I have done this a million times to be sure I am not missing any closing tags or anything else that could cause my problem.
问题是它似乎主要是 HTML4 并且不是为开箱即用的 MVC 设计的,所以我必须稍微调整它才能让它按照我想要的方式工作。(没有什么超出任何非常基本的东西)。例如,将一部分移出索引并使用 Renderbody() 加载它,因此实际的 html 结构永远不会改变。我已经这样做了一百万次,以确保我没有遗漏任何结束标签或其他任何可能导致我的问题的东西。
Up to this point there is no problem at all. Everything loads as it should.
到目前为止,完全没有问题。一切都按其应有的方式加载。
I continued to create a 2nd View and its partial to extract other parts of the site. As usual, baby steps first. Before extracting any other code, I just used a little "Hello World" in the first page, and a similar string in the partial to be sure it was working. It was.
我继续创建第二个视图,并部分地提取站点的其他部分。像往常一样,婴儿先走。在提取任何其他代码之前,我只是在第一页中使用了一个小“Hello World”,并在部分中使用了一个类似的字符串以确保它可以正常工作。它是。
Now when I type in the url Home/ActionName the whole thing reloads as it should but looks horrible. and I get this error message:
现在,当我输入 url Home/ActionName 时,整个内容都会重新加载,但看起来很糟糕。我收到此错误消息:
0x800a1391 - JavaScript runtime error: 'jQuery' is undefined
0x800a1391 - JavaScript 运行时错误:'jQuery' 未定义
Below is my code which clearly defines it:
下面是我的代码,它清楚地定义了它:
<!-- BEGIN CORE PLUGINS -->
<script src="assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function ()
{
App.init(); // initlayout and core plugins
_Layout.init();
_Layout.initJQVMAP(); // init index page's custom scripts
_Layout.initCalendar(); // init index page's custom scripts
_Layout.initCharts(); // init index page's custom scripts
_Layout.initChat();
_Layout.initDashboardDaterange(); //Red date range
_Layout.initIntro(); //Pop up messages
});
</script>
It points me to the jQuery(document).ready part when I see the message.
当我看到消息时,它指向 jQuery(document).ready 部分。
Again, when I load the page normally, it works fine. When I type Home on its own it works fine. Its only when I type Home/AnythingElse that it gives this error message. Even if I type Home/ which should load in the Index file, it gives me this error message.
同样,当我正常加载页面时,它工作正常。当我自己输入 Home 时,它工作正常。只有当我输入 Home/AnythingElse 时,它才会给出此错误消息。即使我输入应该在索引文件中加载的 Home/,它也会给我这个错误消息。
jQuery is defined, so why is this happening on postback?
jQuery 已定义,那么为什么在回发时会发生这种情况?
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by David John Smith
Try setting the src for jQuery to be absolute from the site root:
尝试从站点根目录将 jQuery 的 src 设置为绝对的:
<script src="/assets/plugins/jquery-1.8.3.min.js" type="text/javascript"></script>
Note the /
before assets
- when your src path does not start with a /
the browser will try and load the asset relative to the current path, so in your example when you add the trailing slash to Home
it will try to load jQuery from Home/assets/plugins/...
请注意/
之前assets
- 当您的 src 路径不以 a 开头时/
,浏览器将尝试加载相对于当前路径的资产,因此在您的示例中,当您向其添加尾部斜杠时Home
,将尝试从中加载 jQueryHome/assets/plugins/...
回答by kingPuppy
For me, the MVC bundlers were causing problems(in my case the ui bundler)
对我来说,MVC捆绑器导致了问题(在我的情况下是 ui 捆绑器)
Here is the order which worked for me.
这是对我有用的订单。
<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>
回答by Michael Ela
You need to add the jquery-x.xx.x.js first before the validate.js like this
你需要像这样在validate.js之前先添加jquery-x.xx.x.js
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>