Analyzing login outcomes on a mobile app

I am currently facing an issue with parsing the success function of my ajax while trying to complete a mobile application login. Any assistance would be greatly appreciated.

$(document).ready(function () {
        //event handler for submit button
        $("#btnSubmit").click(function () {
            //collect userName and password entered by users
            var username = $("#username").val();
            var password = $("#password").val();

            //call the authenticate function
            authenticate(username, password);
        });
    });
//authenticate function to make ajax call
function authenticate(username, password) {
    $.ajax
    ({
        type: "POST",
        //the url where you want to sent the userName and password to
        url: "http://my-domain.com/php/jsonserver.php?func=Login",
        dataType: 'json',
        async: false,
        //json object to sent to the authentication url
        data: '{"username"="' + username + '", "password"="' + password + '"}',
        success: function () {
            //do any process for successful authentication here

            }
    })
}

Answer №1

Your issue seems to be related to parsing a status from a web service. Here is some code that might help:

function checkPin(){
        var usernameInput=document.getElementById("uname").value;
        var passwordInput= document.getElementById("pintxt").value;

        $.ajax({
          type:"GET",
          url:"http://hostname/folder/login.php?callback=jsondata&UserName="+usernameInput+"&Password="+passwordInput,
          crossDomain:true,
          dataType:'jsonp',
          success: function jsondata(data)
               {
                    var parsedData=JSON.parse(JSON.stringify(data));
                var loginStatus=parsedData["Status"];

                if("status"==loginStatus)
                {
                    alert("Login successful");
                    window.open("user.html","_self");
                }
                else
                {
                    alert("Login failed");
                    document.getElementById("pintxt").value="";
                    pintxt.focus();
                }
              }  
        }); 
    }

Answer №2

Thank you for the assistance provided.

Here is the snippet of code that I ultimately decided to implement:

function authenticateUser(username, password) {
    $.ajax
    ({
        type: "POST",
        url: URL+"func=Login",
        dataType: 'json',
        async: false,

        data: {username:username,password:password},
        success: function (data, textStatus, jqXHR) { 

                if(data.Result.ErrCode==null)
                {
                    $('.session').html(data.Result.Data[0].sessionid);
                    $('.username').html(data.Result.Data[0].shortname);
                    SESSIONID = (data.Result.Data[0].sessionid);
                    $.mobile.changePage('#main');
                }
                else
                {
                    $('#error').html(data.Result.ErrMsg);

                }

            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('An unexpected error occurred.');

            }
    })
};

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

Using JQuery to access the element within a span

I am completely new to jQuery and still learning as I go. Here is a snippet of the code I'm working on: <div class = 'buttons'> <span> <input type='button' value='BUTTON1' id='button1'> ...

Tips for preparing HTML content with Jquery's "ON" method?

My jQuery Accordion is functioning properly, but I'm encountering issues when trying to load the accordion content dynamically from a database or JSON onto the page. The problem arises because the DOM doesn't have information about the newly inje ...

Steps to connect two drop-down menus and establish a starting value for both

In the scope, I have a map called $scope.graphEventsAndFields. Each object inside this map is structured like so: {'event_name': ['field1', 'field2', ...]} (where the key represents an event name and the value is an array of f ...

How can I add a black-colored name and a red-colored star to the Placeholder Star as a required field?

I'm looking to customize the placeholder text in an input field. I want the main placeholder text to be in black and have a red star indicating it's a required field. However, my attempt to set the color of the star specifically to red using `::- ...

Embed the parent component within the child component

I have two files called Recursive.vue and Value.vue. Initially, when Recursive is the parent component, mounting Recursive within itself works perfectly. Similarly, mounting Value within Recursive and then Value within itself also works fine. However, an ...

What is the best way to extract information from the last 3 tweets using JSON instead of just the last tweet?

I managed to get this code working and it successfully retrieves the last tweet from my brother. However, I would like it to display the last 3 tweets instead. Can someone assist me with this? I am not very proficient in Java or XML, I have just followed a ...

jQuery DataTable error: Attempted to set property 'destroy' on an undefined object

<script> $('#archiveTable').DataTable({}); </script> <table id="archiveTable" datatable="ng" class="table table-sm" style="color:black"> <!--some code--> </table> This is a snippet of HTML code Upon checking t ...

Parsing JSON with PHP when dealing with an array that is not consistent

My current predicament involves receiving data from my android app in the following format: {"uid":1, "newAdd":"New York", "coupon_status":"yes", "coupon_code":"SALE50", "place_code":4, "basket":[{"name":"xyz", "vendorId":1, "total":100, "count":2}, {...} ...

local individuals and local residents (duplicate) dispatched from the server

Upon analyzing my server's response, I have observed a duplicate of my locals within the locals object. Here is an example: Object { settings: "4.2", env: "development", utils: true, pretty: true, _locals: { settings: ...

JavaScript doesn't pause for the data to come back, resulting in an undefined value

When I call the function "classTableData" on a button click, my allData variable becomes undefined. The problem seems to be that it processes the next line of code without waiting for the results, causing allData to remain empty. Can anyone provide some ...

Having an issue with the 'scroll' event not being disabled in jQuery

Currently, we are encountering an issue with the implementation of a simple hiding menu when scrolling and showing it back once the user stops scrolling. The method .on('scroll') works as expected, but .off('scroll') is not functioning ...

Is there a way to sort through nested objects with unspecified keys?

I'm looking to extract specific information from a nested object with unknown keys and create a new array with it. This data is retrieved from the CUPS API, where printer names act as keys. I want to filter based on conditions like 'printer-stat ...

Retrieving the nodeId using Selenium WebDriver with Chrome Remote Interface

While I was successful in using Chrome Remote Interface functions within my Selenium WebDriver session, such as Page.captureScreenshot and Emulation.clearDeviceMetricsOverride, I encountered an issue with invoking methods that operate on DOM elements. The ...

Unable to make an ajax request due to github.io's restrictions

I am facing an issue with all my apps that have ajax requests, as they are returning errors stating: "This request has been blocked; the content must be served over HTTPS." An example of this error can be seen at https://zzharuk.github.io/local_weather_w ...

"Encountering a Reactjs error when trying to render a

Currently, I am engrossed in a React JS tutorial but seem to have hit a roadblock with the following error: Failed to compile. Error in ./src/Components/Projects.js Syntax error: Unexpected token (15:10) return { <ProjectItem key={projec ...

Issue with setting up asp.net ajax control toolkit version 3.5

I've successfully installed the control toolkit (with the dll in the bin folder of my application and the ability to add controls to the toolbox in Visual Studio). However, I'm facing an issue where none of the controls seem to work for me, indi ...

angular ensuring seamless synchronization of objects across the application

This question pertains to both angular and javascript. In our angular app, we have numerous objects from the backend that need to remain synchronized. I am facing challenges in establishing efficient data bindings to ensure this synchronization throughout ...

Leverage Hubspot and Google Analytics to transfer session source and medium data

Is there a way to track session source and medium information in HubSpot to identify where a specific visit originated from before a form is filled out or another conversion event occurs? Ideally, this process would involve transferring the session and me ...

difficulty encountered when passing session variable on PHP pages

In my project, I have two important PHP pages: quizaction.php and result.php. The functionality involves passing variables from quizaction.php to result.php. Below is an overview of my code: <?php include 'db.php'; session_start( ...

You can only remove one cart item at a time

I have created an online shopping website that utilizes AJAX for adding items to the cart and removing them. The strange issue I am facing is with the removal process. I can only delete one item at a time, and then have to manually refresh the page in orde ...