Error encountered in AngularJS: Unexpected POST request

I am facing some challenges when trying to send an http post request to the server. I am using PhoneGap to develop an application that utilizes AngularJS. Can someone guide me on how to make a post request using AngularJS? Here is a snippet of my code:

 .controller('loginCtrl', function($scope, $http , $state, $ionicPopup, AuthService) {
    $scope.login = function(data) {
      AuthService.login(data.username, data.password).then(function TestController($scope) {
        $scope.username = $http({
          url: 'http://samedomain.com/GetPersons',
          method: "POST",
          data: email="email",
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
          $scope.data = data; // how do pass this to $scope.persons?
        }).error(function (data, status, headers, config) {
          alert(error);
          $scope.status = status;
        });

      });
    };

However, upon running this code, I encountered an error message:

Error: Unexpected request: POST http://192.168.30.196:8086/user/login
No more request expected
    at $httpBackend (http://192.168.30.60:3000/lib/angular-mocks/angular-mocks.js:1418:9)
    at sendReq (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:24793:9)
    at serverRequest (http://192.168.30.60:3000/lib/ionic/js/ionic.bundle.js:24503:16)
    ... (additional error details here)...

Answer №1

@user3273700 and @varit05 are absolutely correct. It's crucial to note that the implementation of the TestController as a callback for the AuthService.login() function is incorrect. Understanding how controllers should be properly utilized is essential in this scenario. The login method callback should simply be a standard function without any parameters.

Replace the following code snippet:

AuthService.login(data.username, data.password).then(function TestController($scope) {

with the corrected version:

AuthService.login(data.username, data.password).then(function () {

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

I am looking for the best way to sort my JSON data based on user roles before it is transmitted to the front end using Express and MongoDB. Any

I scoured the internet high and low, but to no avail - I couldn't find any framework or code snippet that could assist me in my predicament. Here's what I'm trying to achieve: whenever a response is sent to my front-end, I want to filter th ...

Remove all the values that start with zero

I've created a function that removes all zero values... How can I modify it to also delete 0 with a plus or minus sign? Currently, only +0 values are being removed. What should I add to my RegEx? Here's my current code: // Transform values ...

Upon pressing the browser's back button, the page fails to automatically load with the updated URL

Providing a little context: Let's say I initially access the page using URL1. After that, I use window.history.pushState() to change the URL to URL2. Now, when I click the "back" button, I observe that the address bar displays URL1 again, but the pa ...

A small computation

How can I modify this code to calculate the total price #ttc by multiplying #totalcout with #marge Currently, I am able to add amounts when checkboxes are clicked, but I am struggling with integrating the multiplication for calculating the Total Price (TT ...

Modify data in JSON using ngModel and then send it via HTTP POST request

Greetings, I am relatively new to Angular and find myself in a bit of a quandary. It seems like the solution to my issue might be simpler than I think. My Angular App consists of a basic "Page View/Controller" and an "Admin View/Controller" that both util ...

A guide to structuring APIs for a web application

I'm currently developing a web application that consists of 7 distinct sections- https://i.sstatic.net/yXLxL.png Each section is related to a courseOrderId, essentially making it an order detail page. So, the question arises: should I utilize a singl ...

Unable to display shadows in Three.js

I've hit a roadblock with the shadow effect at the moment, despite trying various solutions for hours, it's still not appearing. Can anyone point out what I may have done incorrectly? Below is the code snippet for my current scene: //Setting up ...

Methods for validating ajax response with Jasmine

I'm a newbie when it comes to jasmine and I'm trying to figure out how to verify if a specific node is present in the ajax response. Currently, I'm using grunt to run jasmine from the command line and have successfully tested if a function i ...

Utilize the accurate information retrieved from the API and store it in MongoDB

I have a functioning script that automatically sends data to my MongoDB every second, but I am struggling with figuring out how to send the exact data from an API to MongoDB. FULL CODE var requestPromise = require('request-promise'); const ...

Displaying a collapsible table directly centered within the table header

I am having trouble centering my table header in the web browser page. When I click the "+" button, the data is displayed beneath the table header, but I want the collapsible table to be centered directly below the header. I have tried making changes in CS ...

Encompassing object with Mobx observables

I am searching for the best approach to implement @observable on a deeply nested JSON object structure, such as a tree. The data tree can go quite deep, with each node having multiple properties. However, I only need to observe one specific property in eac ...

Saving different data from selected item in JavaScript

I am having an issue with retrieving and saving data from my JS view to my controller and database. I can successfully retrieve the selected items' IDs using item_id. However, when I enter quantities for the selected items, the quantity saved in the ...

managing numerous outdated requests on the server

Within my application, there is a map feature that sends a request to the server whenever a user interacts with it, such as zooming in or panning. The issue arises when users perform rapid actions, leading to multiple requests being sent to the server and ...

Guide on transferring an HTML variable to a PHP script through AJAX

I am currently working on my website where I aim to create a random code that will be passed onto a PHP file for future retrieval. However, the code seems to be malfunctioning. Take a look at the code: Javascript/HTML: function init() { var code ...

The animation functionality for Ionic Vue Router on iOS seems to be malfunctioning

I have recently set up a new Ionic Vue app using the tabs template and encountered an issue after switching the mode to ios - the routing animations disappeared. The animations work fine with md. This is what my index.html looks like: <!DOCTYPE html> ...

The ordering of parameters should not impact the functionality of Typescript generics, though currently it does

My mergeArrays generic function is designed to not rely on the order of parameters. However, when it is used within another generic function, the result seems to depend on the order. This unexpected behavior is puzzling. type Fn = (...args: any[]) => an ...

Problem with AngularJS promise causing incorrect setting of 'this'

I am trying to populate a select list with data from a JSON file. Controller this.languages = []; var getData = $lcidFactory.obtainLCIDS(); getData.then( function(result){ this.languages = result.data; console.log(result.data); }, ...

Encountering a "Cannot read property 'addEventListener' of null" error while working on a dynamic table using JavaScript

I am trying to dynamically create a table on a webpage using JavaScript, but I'm running into some issues. Specifically, I can't use .appendChild and when I ran my script through JSLint, it gave me an error about the 'createTable' funct ...

Troubleshooting Issue with Shopify AJAX Request and Ineffectiveness of e.preventDefault

I am facing an issue with a cart popup that utilizes AJAX to display the cart contents. Within this popup, I have implemented an "X" link to remove specific items from the cart. However, despite using the code below, $('#remove-from-cart').click ...

Sorry, but we couldn't locate the page you were looking for. The robots.txt file seems

Hey there everyone, I'm currently working with Nuxt3 and encountering an error when trying to access the robot.txt file. Here's the Robot.txt module that I've integrated. Within my nuxt.config.ts file: export default defineNuxtConfig({ ...