2017-11-11 131 views
1

我是新來的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; 
     } 
    } 
} 

但在試圖填補我遇到這個錯誤的用戶名和密碼登錄控制檯

enter image description here

有人可以幫到哪裏尋找它我們將不勝感激

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

你可以包括你的服務器代碼的類定義是什麼?另外,很高興看到你正在使用SQL參數考慮你是新的web開發:) – James

+0

@詹姆斯請參閱編輯。你是這個意思嗎?謝謝,但我無法完成工作。 –

+0

你的'LogIn.asmx.cs'中有什麼? –

回答

1

這是一個棘手的一個。我花了一些時間才意識到這個問題。該錯誤表示無法創建Wishlist_2017.Web_Service.LogIn的實例。你提供的文件似乎表明它在那裏它應該是和文件似乎沒問題。

但是,在構造函數上下文中,有這樣的調用:dbconn dbcon = new dbconn();。如果那個失敗了,它可能會導致類型創建失敗,但不是非常具體。

進一步分析,似乎dbconn文件具有初始化連接類似的方式:

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

如果一個出現故障,dbconn創建將失敗,LogIn也將隨之失效。看起來連接字符串有另一個名字,或者有一些配置無效。

嘗試查看是否從dbconn中刪除objConn初始化解決了類型創建問題。

+1

同意,命名空間看起來不錯,只有防止類實例化的東西纔是'web.config'中的'Server'連接字符串。 – James

+0

這就是我的想法:) @James –

+0

我嘗試使用我的web.config上的憑據,但似乎我的憑據是正確的。請參閱我的帖子上的編輯 –

-2

永遠不要手動創建json。與使用任何您使用的語言的json序列化程序相比,它更耗時且更容易出錯。

由於不正確的引號,您正在創建的內容無效!

的物體上使用,而不是JSON.stringify()

data: JSON.stringify({'domain': username , 'accountpassword': password }), 
+0

這不是問題。您正在解決另一個問題(並且如您所見,複製已刪除的答案)。 –

+0

@PatrickHofman必須具有有效的JSON才能開始。刪除的答案仍然使用無效的引號。也許還有其他後端問題,但至少有有效的輸入可以幫助 – charlietfl

0

非常微妙的問題,但您的連接字符串行是問題,即

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

ConnectionStrings['xxx']返回一個對象,而不是字符串本身,你想

string objConn = ConfigurationManager.ConnectionStrings["Server"].ConnectionString; 
+0

的確如此,但這會再現另一個錯誤,就像問題中的錯誤一樣。 –

+0

@PatrickHofman認爲這確實解決了問題中的問題,除非我錯過了另一個錯誤?我只能看到一個。 – James

+0

'ToString'不會讓類的實例化失敗。它仍然重現一個字符串,儘管是無效的。稍後,由於無效的連接字符串,它會失敗,但是在創建類之後很長時間。 –