运行时错误Javascript未定义 [英] Runtime error Javascript is undefined

查看:242
本文介绍了运行时错误Javascript未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

目标:
运行存储过程,并在此情况下确认用户名和密码是否正确。我将要添加插入/编辑到后来的存储过程。

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

  http:// localhost:49647 / Account / Login中第133行第5列未处理的异常

0x800a1391 - JavaScript运行时错误:'$'未定义

想法
我不是一个JS程序员,但会假设我需要添加一些东西来以某种方式淡化值?查看 0x800a1391 - JavaScript运行时错误:'jQuery'未定义 I可能需要打电话?

User.cs

 使用系统; 
使用System.Data.SqlClient;
使用System.Data;
使用System.Configuration;

namespace Login.BL
{
public class User
{
public int UserID {get;组; }
// [必须]
public int用户名{get;组; }
// [必需]
public int UserPassword {get;组; }

$ b public class UserBusinessLogic
{
string conStr = ConfigurationManager.ConnectionStrings [myTaylorWoodrowSQL]。ConnectionString;
public int CheckUserLogin(用户用户)
{
//检查用户是否有效!!!!
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());




code


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

 < script> $($#$ b $(#Login))。click(function()
{
var dataObject = {UserName:$(#UserName).val(),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);
}
},
错误:函数(结果)
{
alert(Error);
}
});
})


解决方案


JavaScript运行时错误: '$'未定义


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

  @ Scripts.Render(〜/ bundles / jquery)

默认的MVC应用程序将使用这个名称绑定的Jquery和验证相关文件〜/ bundles / jquery 这是我们如何使用它,否则,如果它是一个空应用程序,那么你必须创建一个新的包,或者只添加jquery文件的引用到这样的页面。

 < script src =〜/ Scripts / jquery-1.10.2.min.js>< / script> 
//注意只需将文件拖放到页面中,VS将为您提供正确的URL。


As some background I've come via VB6 then VB.Net and am now trying out Bootstrap/MVC with C# so excuse the queries... I've SO searched and been on YouTube as well but a struggling a little. I've my approach is wrong do tell me!

Aim: Stored procedure to run and, in this case, confirm if the username and password are correct. I'll be wanting to add in insert/edit to later Stored Procedures.

Issue: When I run the code on the website I get no error or result. In debug mode I get:

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

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

Thoughts I am not a JS programmer but would assume I'd need to add something to 'Dim' the value in some way? Looking at 0x800a1391 - JavaScript runtime error: 'jQuery' is undefined I might need to call something?

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());
            }
        }
    }
}

Extract from Login.cshtml I can post full code

<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>

解决方案

JavaScript runtime error: '$' is undefined

Means jquery is not loaded into your page. So if its a Default MVC application it would be loaded in the _Layout page. But I think you have written the login page without using the default layout, So you can add this line of code to your login page head section (if you are using a layout page then head section of this layout) and everything must be fine.

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

The default MVC application will have the Jquery and validation related files bundled under this name ~/bundles/jqueryand this is how we use it, Else if its a empty application then you must create a new bundle, Or add only the reference of the jquery file to the page like this.

 <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.

这篇关于运行时错误Javascript未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆