My angular submit function seems to be malfunctioning as it does not add any data or show any

Currently, I am exploring Angular and attempting to utilize the POST method for my REST API calls. Below is the function found in my typeCtrl.js file:

$scope.submit  = function() {
    alert("add new type [" + $scope.newtype + "]" );
    $http.post("http://192.168.1.115:8080/type/add", { 'type': $scope.newtype}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
        console.log(response);
        console.log(response.data);
    });
    $http.get("http://192.168.1.115:8080/type").then(function(response) {
        console.log(response);
        console.log(response.data);
        $scope.all_types = response.data;
    });
};

The line

alert("add new type [" + $scope.newtype + "]" );
successfully triggers as expected, confirming that $scope.newtype contains a value.

Upon reviewing the output from my node.js backend, the following results are displayed:

POST /type/add 200 2.267 ms - 73
GET /type 200 23.854 ms - 6465
GET /type 304 22.217 ms - -
undefined
{ '{"type":"aaaa"}': '' }
POST /type/add 200 1.863 ms - 73
GET /type 200 26.053 ms - 6508
undefined
{ '{"type":"zzzzzzz"}': '' }
POST /type/add 200 1.734 ms - 73
GET /type 200 25.389 ms - 6551
undefined
{ '{"type":"zzzzzzz"}': '' }
POST /type/add 200 2.142 ms - 73
GET /type 200 25.435 ms - 6594

I suspect there might be an issue with how I am setting up the data. Could someone kindly assist me in identifying where I may be going wrong?

Answer №1

My task required me to do the following...

 33     $scope.submit  = function() {
 34         $scope.formdata = "{'type':'"+$scope.newtype+"'}";
 35         alert("add new type [" + $scope.newtype + "]" );
 36         var data = $.param({ type: $scope.newtype });
 37         $http.post("http://192.168.1.115:8080/type/add",
 38             // [{'type': $scope.newtype}],
 39             data,
 40             {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
 41             console.log(response);
 42             console.log(response.data);
 43         });
 44         $http.get("http://192.168.1.115:8080/type").then(function(response) {
 45             console.log(response);
 46             console.log(response.data);
 47             $scope.all_types = response.data;
 48         });
 49     };

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

Having trouble accessing functions in Typescript when importing JavaScript files, although able to access them in HTML

Recently, I started incorporating TypeScript and React into my company's existing JavaScript code base. It has been a bit of a rollercoaster ride, as I'm sure many can relate to. After conquering major obstacles such as setting up webpack correc ...

Utilizing webpack, gulp, and typescript to efficiently incorporate jQuery plugins

I'm having trouble figuring out the correct way to load third-party libraries that have dependencies on each other. I am using TypeScript and Gulp with either Webpack or SystemJS for my module loader, both of which are throwing similar errors in this ...

Comparing dates in JavaScript using the built-in Date object

Currently, I am attempting to compare the current date with the one stored in a database using the code snippet below: <script> $("#registerButton").click(function() { var first = $("#scantime").val(); var second = $("#scanbartime").val(); if (parse ...

Intensive analysis of objects comparing their characteristics

Currently delving into the world of Javascript, I encountered a coding exercise involving a "deepEqual" function that I attempted to write but ultimately struggled with. Even after reviewing the solution, one particular part perplexed me - the for loop, ...

Can JQuery be used to specifically target attributes with vendor prefixes?

Is there a way to change the Thumb color for my slider without needing to select the thumb with a vendor prefix? I want to be able to dynamically pick the color without simply appending a class. $('.button').on('click', function (e) { ...

Load JavaScripts asynchronously with the function getScripts

Asynchronously loading scripts on my login page: $.when( $.getScript("/Scripts/View/scroll-sneak.js"), $.getScript("/Scripts/kendo/kendo.custom.min.js"), $.Deferred(function (defer ...

What is the best approach for rendering content to a page in ReactJS when a user clicks on a dynamic URL?

One challenge I am currently tackling is how to direct users to a specific page when they click on a dynamic URL. Specifically, within my "product_list" API data, there exists a key called "url". Upon clicking this "url", the user should be redirected to a ...

Optimizing AngularJS ui-router to maintain state in the background

Currently working on an AngularJS project that involves a state loading a view containing a flash object. I am looking for a way to ensure that the flash object remains loaded in the background during state changes, preventing it from having to reload ev ...

When incorporating express.static(), the Express .use() callback may be triggered multiple times

I'm in the process of verifying a user's identity, and once that is confirmed I aim to add them as a new user in my personal database using the information provided by the authentication server. The issue at hand is that the function createNewAc ...

What is the purpose of the code snippet 'jQuery.each(lines, function(lineNo, line)'?

As a newbie to jquery and ajax, I stumbled upon some code online that caught my attention. The code snippet below prompted me to question its purpose and functionality: lines = newLine.split('#'); jQuery.each(lines, function(lineNo, line) ...

Executing Angular testing with the command $scope.$apply

What is the reason behind having to use $scope.$apply() in a test case for asynchronous processes to complete? Assume there is a service angular.service("a",function($q){ return { getValue: function(){ return $q.resolve(someObj) ...

Is it possible to deactivate an element based on a specific string present in the URL?

<div class="custom-class1"> <div class="custom-class2" id="custom-id1">hello 1</div> <div class="custom-class3" id="custom-id2">hello 2</div> <div class="custom-class4" id="custom-id3">hello 3&l ...

ReactJS - Experiencing an abundance of re-renders in Protected Route

I have recently started working with React and encountered an issue with my Protected routes. They are functioning perfectly on my local machine but throwing errors on the server. You can see the error https://i.sstatic.net/l6dZW.png Below is my code: Pr ...

Is there a way to trigger the interval on the second load and subsequent loads, rather than the initial load?

I have implemented the use of setInterval in my recaptcha javascript code to address the issue of forms being very long, causing the token to expire and forcing users to refill the form entirely. While I am satisfied with how the current code functions, t ...

Amplify encounters the "Failed to load resource: the server responded with a status of 400" issue

I encountered an error while using Amplify, although the build was completed successfully. Failed to load resource: the server responded with a status of 400 manifest.json:1 The system is functional in the local environment. Below is the Package.json scri ...

Appium with Node.js (wd) becomes unresponsive when unable to locate element

Encountering an issue while using appium with nodejs (wd) and mocha, as there is a loading view in the android app (blackbox testing & I'm not the developer) that needs to be waited for its disappearance. Attempted the following solution: wd.addPromi ...

Extracting the name from a JSON key/value pair and then adding it to a TextField

Here is a sample JSON data: { "Id": 1, "Title": "Information on Jane Smith", "Comments": "Additional details here", "UpdatedBy": "Jane Smith", "UpdateDate": "May ...

custom popup click event in angular leaflet directive does not work as expected on iOS devices

For the development of a map on iOS devices, I am utilizing the angular leaflet directive and ionic. I am facing an issue where the popup is not clickable on the iOS simulator, even though it works fine on web browsers. var html11 = '< ...

Using IF-ELSE statements in jQuery for DataTables

Is there a way to handle If else statements like method in the datatable plugin? Currently, when the 'data' variable returns a value, everything works correctly. However, if it is empty, I would like it to return either "from1" or "from2", which ...

Having difficulty showing custom events on angular full calendar

After pushing events loaded from the server, I am encountering an issue where they are not being displayed on the calendar. Interestingly, the events are in the correct format and can be seen when printed on the page, but for some reason, they do not show ...