Javascript 从 Web 服务获取数据

标签 javascript c# ajax web-services

我是网络应用程序开发的新手,我正在尝试制作一个登录页面并使用 javascript 从本地数据库获取用户数据。但我很难找到我哪里做错了。这是我的 JavaScript 代码

$(document).ready(function () {

$("#log-in-form").on("submit", function (e) {
    e.preventDefault();

    var username = $(this).find("input[type=text]").val();
    var password = $(this).find("input[type=password]").val();

    Authentication(username, password);

});

function Authentication(username,password){
    $.ajax({

        type: "GET",
        url: "../Web Service/LogIn.asmx/get_uinfos",
        data: "{'domain':" + username + "', 'accountpassword':'" + password + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var result = response.d;
            var length = response.length;

            $.each(result, function (index, data) {
                var alias = data.alias;
                window.localStorage.replace("Main.aspx");
            });
        },
        error: function () {
            alert('Function Error "get_uinfos"')
        }
    });
}



});

我正在使用这些代码使用 Web 服务连接到本地服务器

using Wishlist_2017;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services;


namespace Wishlist_2017.Web_Service
{
    /// <summary>
    /// Summary description for LogIn
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LogIn : System.Web.Services.WebService
    {
        dbconn dbcon = new dbconn();

        public class uinfos
        {
            public int id;
            public string alias;
            public string monito;
        }

        static List<uinfos> _get_uinfos = new List<uinfos> { };
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public List<uinfos> get_uinfos(string domain, string accountpassword)
        {
            DataTable table = null;
            SqlCommand cmd = new SqlCommand();

            cmd.CommandText = "Retrieve_UserInfo";

            cmd.Parameters.AddWithValue("@Domain", domain);
            cmd.Parameters.AddWithValue("@Password", accountpassword);

            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            table = this.dbcon.ExecuteDataTable(cmd);

            _get_uinfos.Clear();

            foreach (DataRow row in table.Rows)
            {
                uinfos _list = new uinfos();

                _list.id = Convert.ToInt32(row["id"]);
                _list.alias = row["Alias"].ToString();
                _list.monito = row["Monito"].ToString();

                _get_uinfos.Add(_list);
            }

            return _get_uinfos;
        }
    }
}

但是在尝试通过填写用户名和密码登录时,我在控制台上遇到此错误

enter image description here

有人可以帮忙在哪里看吗,我们将不胜感激

编辑1:

这是服务器类的代码

    using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Wishlist_2017
{
    public class dbconn
    {
        string objConn = ConfigurationManager.ConnectionStrings["Server"].ToString();

        public dbconn()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public DataTable ExecuteDataTable(SqlCommand cmd)
        {
            DataTable dt = new DataTable();

            using (SqlConnection cn = new SqlConnection(objConn))
            {
                try
                {
                    cn.Open();
                    cmd.Connection = cn;
                    cmd.CommandTimeout = 1000;

                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    da.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cn.State != System.Data.ConnectionState.Closed)
                        cn.Close();
                }

                return dt;
            }
        }

        public void ExecuteNonQuery(SqlCommand cmd)
        {
            using (SqlConnection cn = new SqlConnection(objConn))
            {
                try
                {
                    cn.Open();
                    cmd.Connection = cn;
                    cmd.CommandTimeout = 1000;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cn.State != System.Data.ConnectionState.Closed)
                        cn.Close();
                }
            }
        }


        public object ExecuteScalar(SqlCommand cmd)
        {
            object result = null;

            using (SqlConnection cn = new SqlConnection(objConn))
            {
                try
                {
                    cn.Open();
                    cmd.Connection = cn;
                    cmd.CommandTimeout = 1000;
                    result = cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cn.State != System.Data.ConnectionState.Closed)
                        cn.Close();
                }
            }

            return result;
        }
    }
    }

连接字符串在我的 web.config 中定义

编辑2:

这是我的 web.config 上的连接字符串

<connectionStrings>
  <add name="Server" connectionString="Data Source=(LocalDB)\ArnServer; initial Catalog=Wishlist; uid=sa; pwd=ordiz@2017!; Asynchronous Processing=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

最佳答案

这是一个棘手的问题。我花了一些时间才意识到这个问题。该错误表示无法创建 Wishlist_2017.Web_Service.LogIn 的实例。您提供的文件似乎表明它位于应有的位置,并且文件看起来没问题。

但是,在构造函数上下文中,有这样的调用:dbconn dbcon = new dbconn();。如果失败,可能会导致类型创建以一种不太具体的方式失败。

进一步分析,dbconn文件似乎有类似的初始化连接的方式:

string objConn = ConfigurationManager.ConnectionStrings["Server"].ToString();

如果失败,dbconn 的创建将失败,LogIn 随后也会失败。连接字符串似乎有其他名称或某些配置无效。

尝试查看从 dbconn 中删除 objConn 初始化是否可以解决类型创建问题。

关于Javascript 从 Web 服务获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47238111/

相关文章:

c# - FoxPro DBF 表转换为 MySQL 表 C#

c# - C# 中的表达式.Like

c# - Autofac 参数化实例化,对不同的参数有不同的解析

ajax - 页面加载时多次 AJAX 调用

javascript - ajax 和 php 文件从输入上传 undefined index

javascript - JS : why does this return true?

javascript - 如何在 onClick 事件的函数中传递参数

php - 如何将多个 PHP 变量传递给 jQuery 函数?

javascript - 在页面加载或刷新时加载随机js文件

javascript - 在 React Native 中使用变量作为对象名称