Why is my data not being sent with the $http POST request in AngularJS?

using angularjs to fetch data with $http

userId = '1';

$http({
url: "php/loadTab.php",
method: "POST",
data: userId
}).success(function(data, status, headers, config) {
    console.log(data);
}).error(function(data, status, headers, config) {
});

Encountering issues - POST method not sending data to php. When trying to echo $_POST['userId'], it returns undefined index. Attempted using data:'userId':'1' as well.

Answer №1

When working with Angular, data is sent as JSON instead of form encoded key/value pairs. This means you can set the data to an object. Your second attempt is close, but don't forget to include the curly braces to define the object:

data: { 'userId' : userId }

On the PHP side, you will need to access the raw POST data to decode the JSON like this:

$data = json_decode(file_get_contents('php://input'));
echo $data->userId;

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

Recovering antiquated submission script in JavaScript

Here is a form with input data: <form id = 'myform'> ... <td><input type="checkbox" name="supplier_aid" value="on" checked disabled >{$output.t_artikelnr}</td> <td><input type="checkbox" n ...

What is the best way to combine a require statement and a function in CoffeeScript?

I'm trying to accomplish this task: require('./config/enviroment.js')(app, express); However, I am unsure of the proper method... I attempted: require './config/routes.js'(app, routes) -> Resulting in: require('./conf ...

Creating an interactive table with the power of FPDF and PHP

I have developed a unique Invoice System that allows users to customize the number of headings and fields using FPDF in conjunction with PHP. Subsequently, I can input the heading names and field values into HTML input fields. However, I am encountering a ...

Implementing SVG in NextJS 13 with custom app directory: A step-by-step guide

Recently, I decided to explore the app directory and unfortunately ran into some issues. One of the main problems I encountered was with image imports. While PNG images imported without any problem, SVG images seemed to break when importing in /app. For i ...

Adding a fresh PHP object to an array of objects - a step-by-step guide

Within my code, I have a user class that contains a function specifically designed for adding users. In addition to the user class, there is also a form that triggers an AJAX call upon submission. This call passes a query string containing the username an ...

Include a variable in an array object?

Yesterday I had some assistance with looping through my var arr to search the system for .png files. The code snippet used was: const fs = require('fs'); const path = require('path'); const imageDir = '/var/scraper/public/image ...

What is the best way to optimize angular code for production environments?

My development project, utilizing Node and Angular, is ready to go into production. How can I minify the Angular code for production? I am looking to minify or uglify the following controller. Whenever I try to minify the code online, the application stop ...

Issue with JQuery on("submit") event not triggered upon first click

I'm currently using JQuery to check the size of an input field after submitting a form: <!DOCTYPE html> <html lang="en> <head> <title>Page 1</title> <meta charset="utf-8"/> <script src="https://ajax.google ...

Can you navigate between slides with JQuery?

Looking to create a website similar to . The only obstacle I am encountering is understanding the code. I prefer using JQuery over MooTools. Is there anyone who can kindly assist me by providing a demo? I want to implement the functionality of switching b ...

The Less compiler (lessc) encounters an issue on a fresh operating system. ([TypeError: undefined is not a function])

After setting up my new development environment on Windows 10, I encountered an issue with less. Following the instructions on lesscss.org, I installed less using: npm install -g less The installation process completed without any errors. However, when ...

Comparison of Ajax and submit: Why am I not receiving identical responses?

Utilizing Ajax for submission and all global variables ending in box. Note that the initialization code is functioning correctly. function begin() { if (confirm("Proceed at your own risk as all consequences are yours!!")) { var usernameNa ...

"Implementing an Ajax request to loop through a JSON

I have received these results and now I want to display them in my view: val: {email: Array(1), first_name: Array(1)} email: Array(1) 0: "The email field is required." length: 1 __proto__: Array(0) first_name: Arra ...

I'm wondering how I can design a utility function within my Redux module that can extract a specific subset of read-only data from the current state

I am currently utilizing redux to create a "helper function" inside my redux module that is responsible for fetching filtered data from the state based on a specified index. This specific data will be used to generate a form consisting of inputs depending ...

Conditionally Add Columns to jQuery Datatables

I am working with a jQuery datatable to showcase data retrieved through an AJAX request. However, I am interested in adding a third column to the table specifically for administrators, allowing them to delete entries. Can someone guide me on how to incorpo ...

Problem with Bootstrap loading state when certain input fields are required

Just starting out with Bootstrap 3 and looking to integrate the loading state button function into my form. I've set up an input field with the required option as shown below. <form> <input class="input" name="title" type="text" required / ...

A guide on retrieving data from an API and displaying it using AngularJS

REACT $state.saveData= function(productfilter){ var url = CONFIG.apiUrl + '/product'; window.open(url); window.print(url); }; CSS <button onClick="saveData(productfilter)" type="button">Print</button> ...

In my submit function, I am utilizing one Ajax call to retrieve data. I am interested in merging the data from this Ajax call with the data from a second Ajax call before submitting

Starting with an initial AJAX post to upload a file, now the goal is to have the same AJAX function grab data from another AJAX call and post it together - essentially posting the ID back to the database. Everything is functioning correctly, but the challe ...

Tips for retrieving the checked status of a Material UI <Checkbox/> component and changing it to false upon clicking a different icon (such as a delete icon)

Greetings! I have encountered a problem that I am hoping someone can assist me with. I am looking for information or guidance on how to handle a specific issue. The challenge I am facing involves accessing the checked property of a material-ui <Checkbo ...

`Datatables sorting feature for British date and time`

I'm currently attempting to organize a column in a table using the DataTables plugin that contains UK date and time formats such as: 21/09/2013 11:15 Implementing Ronan Guilloux's code: jQuery.extend( jQuery.fn.dataTableExt.oSort, { "uk_dat ...

Updating the jQuery $ function to accommodate outdated codebases

After using stackoverflow as a valuable resource for years, I decided to join. I have a good grasp on JavaScript and noticed some questions that I could provide input on. As I delved into the platform, I realized the prevalence of jQuery. This prompted me ...