javascript ASP.NET MVC - 未捕获的 ReferenceError:即使我包含了库,也未定义 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28959362/
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
ASP.NET MVC - Uncaught ReferenceError: jQuery is not defined even if I'm including the library
提问by Traffy
I'm using ASP.NET MVC 4 and I'm trying to pass a value from my JavaScript to my Controller Action. Here's what I'm trying to do in my JavaScript :
我正在使用 ASP.NET MVC 4 并且我试图将一个值从我的 JavaScript 传递到我的控制器操作。这是我在 JavaScript 中尝试执行的操作:
function DoubleClick() {
debugger;
var id = 1;
$.ajax({
type: "POST",
url: "/Home/GetAppId",
data: { id: id}
});
}
And my action (really simple action) :
而我的行动(非常简单的行动):
public JsonResult GetAppId(int id)
{
//some code will be included here
}
In my layout template, I'm including correctly my scripts (actually I'm using JS in some ohter pages and it works) :
在我的布局模板中,我正确地包含了我的脚本(实际上我在一些其他页面中使用了 JS 并且它有效):
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.validate.globalize.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/jquery-ui-1.10.4.custom.min.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-ui-1.10.4.custom.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/DatePickerReady.js")" type="text/javascript"></script>
When I'm opening the Chrome dev tool (F12), I'm seeing the following error message in the console tab :
当我打开 Chrome 开发工具 (F12) 时,我在控制台选项卡中看到以下错误消息:
Uncaught ReferenceError: jQuery is not defined
未捕获的 ReferenceError:未定义 jQuery
I can't really see where the problem is. Any idea?
我实在看不出问题出在哪里。任何的想法?
回答by Dawood Awan
jQuery.Validate uses jQuery.
jQuery.Validate 使用 jQuery。
<script src="@Url.Content("~/Scripts/jquery.validate.globalize.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
Should be like this:
应该是这样的:
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.globalize.min.js")" type="text/javascript"></script>
As to why your JavaScript is not posting back the Value that is another error.
至于为什么您的 JavaScript 没有发回另一个错误的值。
In Ajax you using "POST". Put a HttpPost Attribute on your method.
在 Ajax 中,您使用“POST”。在您的方法上放置一个 HttpPost 属性。
[HttpPost]
public JsonResult GetAppId(int id)
{
//some code will be included here
}