【问题标题】:Runtime error Javascript is undefined运行时错误 Javascript 未定义
【发布时间】:2016-06-15 06:52:27
【问题描述】:

作为一些背景,我是通过 VB6 和 VB.Net 来的,现在我正在尝试使用 C# 的 Bootstrap/MVC,所以请原谅这些查询......我已经搜索过并且也在 YouTube 上,但有点挣扎。我的方法是错误的请告诉我!

目标: 要运行的存储过程,在这种情况下,确认用户名和密码是否正确。我想在以后的存储过程中添加插入/编辑。

问题: 当我在网站上运行代码时,我没有收到任何错误或结果。在调试模式下,我得到:

Unhandled exception at line 133, column 5 in http://localhost:49647/Account/Login

0x800a1391 - JavaScript runtime error: '$' is undefined

想法 我不是 JS 程序员,但会假设我需要以某种方式添加一些东西来“暗淡”价值?看着0x800a1391 - JavaScript runtime error: 'jQuery' is undefined 我可能需要打电话?

User.cs

using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; 

namespace Login.BL
{
    public class User
    {
        public int UserID { get; set; }
        //[Required]
        public int Username { get; set; }
        //[Required]
        public int UserPassword { get; set; }

    }
    public class UserBusinessLogic
    {
        string conStr = ConfigurationManager.ConnectionStrings["myTaylorWoodrowSQL"].ConnectionString;
        public int CheckUserLogin(User User)
        {
            //Check the user is valid!!!!
            using (SqlConnection conObj = new SqlConnection(conStr))
            {
                SqlCommand comObj = new SqlCommand("retrieveUserByNameAndPassword", conObj);
                comObj.CommandType = CommandType.StoredProcedure;
                comObj.Parameters.Add(new SqlParameter("@Username", User.Username));
                comObj.Parameters.Add(new SqlParameter("@Password", User.UserPassword));
                conObj.Open();
                return Convert.ToInt32(comObj.ExecuteScalar());
            }
        }
    }
}

Login.cshtml中提取我可以发布完整代码

<script> 
    $("#Login").click(function ()
    {
        var dataObject = { UserName: $("#UserName").val(), Password: $("#Password").val() };
        $.ajax({
            url:'@Url.Action("Login","User")',
            type:"POST",
            data: dataObject,
            datatype: "json",
            success: function(result)
            {
                if(result.toString()=="Success")
                {
                    alert(result);
                }
                else
                {
                    alert(result);
                }
            },
            error: function (result)
            {
                alert("Error");
            }
        });
    })
</script>

【问题讨论】:

  • 你的脚本前是否包含 jquery?
  • 你是否为 jquery 添加了&lt;script&gt; 标签,类似于&lt;script src='/scripts/jquery.min.js"&gt;&lt;/script&gt;

标签: javascript html


【解决方案1】:

JavaScript 运行时错误:“$”未定义

表示 jquery 未加载到您的页面中。因此,如果它是一个默认 MVC 应用程序,它将被加载到 _Layout page 中。但是我认为您已经编写了登录页面而没有使用默认布局,因此您可以将这行代码添加到您的登录页面头部部分(如果您使用的是布局页面,那么这个布局的头部部分)并且一切都必须没问题。

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

默认的 MVC 应用程序会将 Jquery 和验证相关文件捆绑在此名称 ~/bundles/jquery 下,这就是我们使用它的方式,否则如果它是一个空应用程序,那么您必须创建一个新包,或者仅添加像这样将 jquery 文件添加到页面中。

 <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
//note just drag and drop the file into the page and VS will do the work for you of putting the proper URL there.

【讨论】:

  • 谢谢。我使用了默认的 MVC 布局(或者我认为我这样做了!),但添加了上述两种布局,它似乎正在工作!谢谢
  • 有没有更好更干净的例子我应该使用@Reddy?
  • 不要同时添加,Jquery 应该只加载一次。否则在使用某些插件时可能会导致问题,或者可能会影响您的自定义代码等。
  • @indofraiser 如果您正在寻找 MVC 的开始,那么我认为这对您来说是一个很好的参考。codeproject.com/Articles/866143/Learn-MVC-Project-in-days-Day
【解决方案2】:

在运行脚本之前,您似乎没有加载 jquery 库。

有关如何包含它的详细信息,请参阅https://learn.jquery.com/about-jquery/how-jquery-works/

最好是获取 jquery.js 的本地副本,然后插入一行如

<script src="jquery.js"></script>

在脚本标签之前加上你自己的代码。

【讨论】:

    猜你喜欢
    • 2013-07-12
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2017-11-27
    • 2013-04-03
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多