0
votes

For some reason this AJAX program is not accepting my json object.

I have a mdf file accessed by registerdb that I am sure is correct.

It has 3 columns: ids, username and password filled with data.

Any help would be appreciated.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= btn.ClientID %>").click(function () {
            var User = $("#<%= txtuser.ClientID %>").val();
            var Pass = $("#<%= txtpass.ClientID %>").val();
            var data = "{ Username: " + User + ", Password: " + Pass + " }"
            var json1 = JSON.stringify(data);
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/Login",
                data: json1,
                dataType: "json",
                success: function (result) {
                    $("#<%= txtid.ClientID %>").val(result.d);
                },
                error: function (Msg) {
                    $("#<%= lblError.ClientID %>").text('failed:' + Msg.status + ':' + Msg.responseText);

                }
            }); return false;
        }); 
    });
</script>

Default.aspx.cs

[System.Web.Services.WebMethod]
public static string Login(string Username,string Password) 
{
    string ids="";
    string connStr = ConfigurationManager.ConnectionStrings["registerdb"].ConnectionString;
    string cmdStr = "SELECT [ids] FROM [register] WHERE [username]=@username AND [password]=@password;";
    try
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@username", Username);
                cmd.Parameters.AddWithValue("@password", Password);
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    if (rdr.Read())
                    {
                        ids = rdr[0].ToString();
                    }
                }
                if (ids == "") { ids = "No Match"; };
                conn.Close();
                cmd.Dispose();
                conn.Dispose();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    } 
    return ids;  
}

Error code:

failed:500:{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027System.Collections.Generic.IDictionary`2[System.String,System.Object]\u0027","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

1

1 Answers

1
votes

Your problem is that the data you're passing into JSON.stringify is a string, not a JSON object. This is how you should be doing it:

var data = {Username:User, Password:Pass};
var json1 = JSON.stringify(data);