The information being sent from Angular is not being successfully transmitted to the XAM

Here is my Angular service post method:

getUserDetails(username , password)
{
  alert(password);
  return this.http.post<myData>("http://localhost/test/api/auth.php", {
  username, password
});

}

This is the structure of my PHP file:

<?php 
  header("Access-Control-Allow-Origin: http://localhost:4200");
  header("Access-Control-Allow-Headers: Content-Type");
  if(isset ($_POST) && !empty($_POST)){
  $username=$_POST['username'];
  $possword= $_POST['password'];
  if($username == 'admin' && $password=='adim'){
    ?>
    {
        "success": true,
        "secret": "This is the secret no one knows but the admin"
    }
    <?php } else {
        ?>
        {
            "success": false, 
            "message": "invalid credentials"
        }
        <?php 
    }
    }else {
    ?>
    {
        "success": false,
        "message": "Only POST access accepted"
    }
    <?php 
 }
?>

Although the PHP file is being called, the username and password data are not being posted. The first if block is not triggered at all, only the else block is executed. When I remove the first if statement, then the result changes to:

   <br />
   <b>Notice</b>:  Undefined index: username in 
   <b>C:\xampp\htdocs\test\api\auth.php</b> on line <b>5</b><br />
   <br />
   <b>Notice</b>:  Undefined index: password in 
   <b>C:\xampp\htdocs\test\api\auth.php</b> on line <b>6</b><br />
        {
            "success": false, 
            "message": "invalid credentials"
        }

Answer №1

To retrieve data from the php side, you can use the following code snippet...

$data = json_decode(file_get_contents("php://input"));

Within $data, you will find your object.

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

Ways to Insert Text and Values into an Array

{{ "user": "randomuser", "message": "require assistance" }, { "user": "automated assistant", "message": "do you need any help?" }, { "user": "randomuser", "message": "another inquiry" } I am seeking to extract additional paragraphs ...

angular restore reactive form

After sending my form, I am attempting to reset it but only the value is set to null. component.html <div *ngIf="!loading" fxLayout="row" class="note-textarea"> <form fxFlex fxLayout="column" fxLayoutGap="10px" [formGroup]="noteForm"> ...

Issue: The value of an object is not defined (evaluating '$scope.inputcode.password')

HTML Form: <form ng-submit="mylogin()"> <div class="list"> <label class="item item-input"> <span class="input-label">Username</span> <input type="text" ng-model="inputcode.username"> ...

Cheerio - Ensure accurate text retrieval for selectors that produce multiple results

Visit this link for more information https://i.stack.imgur.com/FfYeg.png I am trying to extract specific market data from the given webpage. Specifically, I need to retrieve "Sábado, 14 de Abril de 2018" and "16:00". Here is how I did it using Kotlin an ...

"Utilizing AJAX to set an array as a global variable

Struggling with storing data values from an AJAX response XML into a global array, and then attempting to call a function that removes specific elements from it. The issue lies in the fact that the array is not declared as global. Here's the current c ...

Encountered a module build error while upgrading Angular Project from version 14 to 15

When attempting to run my project, an error is displayed. ./src/styles.scss?ngGlobalStyle - Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): HookWebpackError: Module build failed (from ./node_modules/sass-loader/dist ...

Looping through an array of JSON objects in Javascript results in finding instances, however, the process records them

Currently, I am executing a script inside a Pug template. The script commences by fetching an array of JSON objects from MongoDB. I then stringify the array (data) and proceed to loop through it in order to access each individual JSON object (doc). Subsequ ...

Transferring form state information between parent and child elements

In my development project using Angular 2, I encountered an issue with managing a long and complex form by splitting it into a parent component and two child components for better organization. The challenge arose when I needed to track the form status for ...

Having trouble serving static files with express.Router?

With my file system becoming more complex, I decided to switch from using app.use(express.static()) to utilizing express.Router(). Initially, I thought I could just replace app.use(express.static()) with router.use(express.static()), but unfortunately, thi ...

Having trouble updating Angular CLI

It appears that I have successfully installed version 9 as per the usual installation process. npm install -g @angular/cli npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9abbca8acbcaaad99ebf7e1e1f7eb"> ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...

Ways to transmit information among Vue components

I am trying to figure out how to pass data from the Module variable in CoreMods.vue to ExternalWebpage.vue using Vuex. I want the CoreModule state to be watched for changes in the external webpage. This is my store.js setup: import Vue from 'vue ...

Creating a dynamic progress bar that scrolls for multiple elements

I am currently working on implementing a scrolling progress bar to show users how much of an article within a div they have read. For reference, something similar can be seen on this site. I have created my custom progress bar and coded it on fiddle, whe ...

What causes Vue to only update once when there are two closely timed mutations to reactive data?

Can you take a look at this simple example? export default { data() { return { name: "Amy", age: 18, }; }, computed: { combinedDataForWatching() { return { name: this.name, age: this.age, ...

Designing motion graphics for a browser game

As I delve into learning about Node.js, Angular.js, Socket.io, and Express.js, my current project involves creating a multiplayer poker game like Texas Hold 'Em. However, despite spending a considerable amount of time browsing the internet, I have bee ...

A comprehensive guide to leveraging synchronous execution of setTimeout in JavaScript

Can the desired output shown below be obtained using setTimout? If it is possible, please provide your insight: console.log("1st"); setTimeout(() => { console.log("2nd"); },0); console.log("3rd"); The expected output should be: 1st 2nd 3rd ...

The process of JavaScript for detecting and extracting text from

After much searching, I stumbled upon this intriguing piece of JavaScript code at this location. Curiously, I was able to remove a few eggs using the following script: var eggCoordinates = []; for (var i = 0; i < 5; i++) { eggCoordinates.push(new E ...

Having trouble retrieving the value of the second dropdown in a servlet through request.getParameter

I am facing an issue with storing the value of the second dropdown in a servlet after utilizing an ajax call in Java to populate it based on the selection made in the first dropdown. While I was able to successfully store the value of the first dropdown ...

What are the steps to reduce the node version?

How can I downgrade my npm version from 16.13.1 to 12.0.1? Any guidance would be greatly appreciated! Thank you in advance. ...

Tips for managing and modifying information with a dropdownlist in asp.net core mvc and angular js

In my asp.net core MVC project, I am incorporating AngularJs to manage two dropdown lists and a textbox. While the textbox functionality for saving and editing data works well, I am facing issues with resetting the dropdown lists after posting data and not ...