我是新来的Web应用程序开发我想做一个登录页面,并从本地数据库使用JavaScript获取用户的数据。但是我很难找到我做错事的地方。下面是我的javascript代码Javascript从Web服务获取数据
$(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;
}
}
}
但在试图填补我遇到这个错误的用户名和密码登录控制台
有人可以帮到哪里寻找它我们将不胜感激
EDIT 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; [email protected]!; Asynchronous Processing=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
你可以包括你的服务器代码的类定义是什么?另外,很高兴看到你正在使用SQL参数考虑你是新的web开发:) – James
@詹姆斯请参阅编辑。你是这个意思吗?谢谢,但我无法完成工作。 –
你的'LogIn.asmx.cs'中有什么? –