Extract information from a Web Service using Javascript

As a novice in web app development, I am currently working on creating a login page and retrieving user data from a local database using JavaScript. Despite my efforts, I seem to have made an error somewhere in my code. Below is the snippet of my JavaScript code:

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



});

Additionally, I'm establishing connection with the local server through a web service using the following code:

using Wishlist_2017;
using System;
...

Upon attempting to log in by entering the username and password, I encountered an error visible on the console.

https://i.sstatic.net/yw6Cz.png

If anyone could provide guidance on where to troubleshoot this issue, it would be greatly appreciated.

EDIT 1:

This is the server-side class code:

    using System;
using System.Collections.Generic;
...

The connection string is configured within my web.config file.

EDIT 2:

Below is the connection string as defined in my 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>

Answer №1

This problem was quite challenging to unravel. It took me a while to identify the root cause. The error message indicated a difficulty in creating an instance of the Wishlist_2017.Web_Service.LogIn. Upon examining the provided files, it seemed that the necessary file was present and appeared to be correct.

Upon closer inspection, I noticed a crucial line within the constructor: dbconn dbcon = new dbconn();. If this line encounters an issue, it could lead to a failure in type creation, albeit not in a clearly defined manner.

Further analysis revealed that the initialization process in the dbconn file follows a similar pattern:

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

If this step fails, the creation of dbconn will also fail, consequently causing the failure of LogIn. It appears that the connection string may have a different name or there could be an issue with the configuration.

I recommend attempting to remove the objConn initialization from the dbconn file to see if this resolves the type creation problem.

Answer №2

There is a minor but crucial issue with your code involving the connection string line:

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

The correct way to retrieve the connection string is as follows:

string connString = ConfigurationManager.ConnectionStrings["Server"].ConnectionString;

Answer №3

Avoid hand-crafting json data at all costs. It's much more efficient and less error-prone to utilize a json serializer specific to the language you're using.

Your current method is generating invalid json due to incorrect quotes!

Instead, consider using JSON.stringify() on an object like this:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Reduce the use of javascript by incorporating NPM specifically after publishing instead of during the building process

My goal is to minify JS files using NPM commands, but I want the minify command to only run after Post Publish and not on Build. Unfortunately, it currently runs after both build and publish. In my Package.json file, I have the following code: "scripts" ...

How can I deactivate a Material UI button after it has been clicked once?

Looking to make a button disabled after one click in my React project that utilizes the MUI CSS framework. How can I achieve this functionality? <Button variant="contained" onClick={()=>handleAdd(course)} disabled={isDisabled} > ...

Adjust the object's width and position based on the window's width

I am currently attempting to adjust the width of a box and the position of a btn based on the size of the window. Q1. How can I eliminate the excess white space located behind the scroll bar? (I have already set it to 100%..) Q2. After clicking the ...

Unlocking the Power of Marionette.CompositeView: Passing Parameters to Marionette.ItemView

Is there a way to access the app.vent from Marionette.ItemView? One solution might be to pass a parameter (app.vent) to Marionette.ItemView from Marionette.CompositeView. Here's the code snippet: // view/compositeView.js define([ 'marionet ...

Utilize the power of Facebook login in your Parse client side application by integrating it with the user object

Currently, I am in the process of implementing a login system using both the Parse and Facebook Javascript SDK. While I have successfully implemented authentication on the client side, I am now facing the challenge of accessing the user object (generated ...

Anticipating the fulfillment of promises with the power of Promises.all

I have adopted the code found here for loading multiple .csv files. Upon successful loading of these files, my intention is to then render a react component. Below is the method located in datareader.js that I am currently working with. I am exploring the ...

Leveraging Prototype's Class creation function to declare confidential and safeguarded attributes and functions

Looking for a solid approach to defining private and protected properties and methods in Javascript? Check out this helpful discussion here on the site. Unfortunately, the current version of Prototype (1.6.0) doesn't offer a built-in method through it ...

Refreshing the page results in a 404 error when utilizing React Router

I am currently facing an issue with my web application setup. Back-End My back-end consists of a Node.js/express server that serves files in response to specific requests made to certain routes. Front-End On the front-end, I have React pages that commu ...

Experiencing issues with ng-repeat in AngularJs?

I am currently facing an issue with two tables that are rendering data through AngularJS from two separate C# methods. Both tables have almost identical structures, with the first one being used as a search field and the second one to display names. The pr ...

Issues with the script manager functionality in asp.net

I need to display a message to the user after submitting a form and include the ID received from the database in the message like this: int reqid = getIDFromDatabase(); string scrp = "<script>alert('Your request has been submitted successfully ...

Is it possible to substitute a one-line jQuery.load() with a fetch() function that achieves the same result?

I am currently working on a page where I utilize a single line of jQuery code: $('#id').load('/url'); This line allows me to load a fragment into a specific place in the DOM. However, I am now considering reducing my reliance on jQuer ...

The issue arises when attempting to use input alongside debounce, event.persist(), and storing the value at the parent component

Is there a way to implement an input field with debounced search where the value is passed from the parent component? Currently, when I pass the value from the parent component it doesn't work as expected. What would be the correct approach to make th ...

PostBackUrl="javascript:void(0);" prevent postback from occurring for all controls within the webpage

Is there a way to prevent postback for a specific control on the page? I've managed to achieve this successfully. However, after clicking on that control, all other controls on the page do not trigger any postbacks. Everything was working fine until I ...

Steps for extracting a portion of the current page's URL

Can someone help me extract a specific part of the current URL using JavaScript? The URL looks something like this: I need to isolate the number "3153038" from the URL and store it in a JavaScript variable. Thank you! ...

Searching for first and last names in MySQL

So, I'm working with a database table named players that contains data for each player including their forename and surname. I've also implemented a PHP Ajax search feature to allow users to look up players. For instance, if someone types in "Jam ...

The process of initializing the Angular "Hello World" app

Beginning with a simple "hello world" Angular app was going smoothly until I attempted to add the controller. Unfortunately, at that point, the expression stopped working on the page. <!doctype html> <html ng-app='app'> <head> ...

The AJAX form fails to submit the information, causing PHP to not process the request

I encountered an issue with this particular form a few days ago which was resolved using a different PHP code provided by the helpful folks here. However, after making a small change to the form, it no longer works. I have verified that all the variables b ...

The Nuxt.js landing page is having trouble loading, but all other components are functioning perfectly

I successfully created a blog using Nuxtjs and everything was working perfectly in development mode. However, when I deployed the application to my cPanel server, the landing page (index.vue) at showed an error message saying This page could not be found ...

Switching on the click functionality

I was able to successfully toggle a boolean variable along with some other options. However, I encountered an issue when trying to create another click function for a different button to set the boolean variable to false. Here is my code: let manageme ...

Dealing with Slow Loading and Partial Data Returns in .NET Core and Ajax

Out of a total of 6 records in the database, only 4 are returning successfully while the remaining 2 are unsuccessful. Upon checking the controller, I found no issues with the data. Additionally, the loading speed seems to be slow despite the small number ...