Efficiently transmitting multiple data sets with AngularJS through a single request

As I develop a simple examination application using Angular for the frontend, I have been struggling with a particular issue for the past two days. The problem revolves around making a POST request where the data to be posted comes from iterated data in my view (specifically, question.id and tempanswer through ng-repeat directive). While I know how to make single requests, this specific scenario has me stumped.

Take a look at the code snippet below:

Backend:

(Code snippet provided for backend) 

Angular (frontend):

(Code snippet provided for Angular/frontend)

View (HTML):

(Code snippet provided for HTML/view)

I have tested my API using Postman and it works correctly with the specified format. However, when used in my backend, the tempAnswer returns null. I suspect the issue lies within my Angular setup. Any suggestions or solutions would be greatly appreciated. Since I am new to Angular, I'm eager to learn and improve.

Answer №1

After debugging a bug in my app, I made some changes to both the view and model (Angular side) that might help others facing similar issues.

In the Angular side, I declared an empty object as a container for answers and question IDs.

$scope.answers={}; // declaration at global level.

$scope.mobileSubmitExam = function() {

        var result = $window.confirm('Are you sure you want to submit?');
        var submitInfo = {
            'answers': JSON.stringify($scope.answers),
            'student_id': $scope.studentDetail.id,
            'subject_slug': $scope.subject_slug,
            'subject_id': subject_id
        };
        console.log(submitInfo);
        if(result == true) {

            $http.post(BASE_URL + 'mobile/examination/submit', submitInfo)
                .success(function(response) {
                    $scope.answers = '';
                    console.log(response);
                })
                .error(function() {
                    $window.confirm('Error occurred while submitting the examination!');
                })
        }
    };

And in the view:

<input type="text" name="answers" id="answers" ng-model="answers[detail.id]" class="validate"> // After change.

<input type="text" name="answers" id="answers" ng-model="answers" class="validate"> // Before change.

That's all. Thank you for all the comments and reactions, I truly appreciate it.

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

Retrieving the value of a JavaScript variable from an HTML document using Python

I am looking to extract the value of myJSONObject from an HTML document that includes javascript code with a JSON object. Here is an example snippet: <html> <head> </head> <body> <script type="text/javascript"> ...

Expiration Alert: SSL Certificates for HERE Links will expire shortly

Our SSL certificates for the following URL's are approaching expiration: *.base.maps.ls.hereapi.com geocoder.ls.hereapi.com Do you have any information on when these URLs will be updated with new certificates? ...

What is the best way to integrate a JSON response obtained from $http.get in Angular?

When retrieving a JSON using $http.get, I am facing compatibility issues with Angular. To make it compatible, I have to convert it like this: $http.get('/api/v1.0/plans'). success(function(data) { var plans = []; for(var prop ...

Store the active tab in AngularJS with Bootstrap to easily remember and display

After creating a basic AngularJS application with the Bootstrap directive, I noticed that some of my pages have tabs. The issue arises when I am on a tab other than the first one and click a link to navigate to another view. Upon returning (using either th ...

What is the best way to incorporate a Video Player into React-JSX?

I am brand new to React/JSX and coding in general. Please excuse any lack of clarity in my question. Currently, I am working on a web app project for an exam. I need to add a video player, but I am struggling to find one that fits with my code. I am using ...

The use of an import statement outside a module in the /node_modules directory is prohibited

As I embark on migrating some node packages, I find myself facing a significant challenge. Here is the current state of my package.json file: { //These are the updated dependencies and Jest configurations "dependencies": { "@google-cloud/logging-w ...

An inconsistency with the return value of promises in $httpBackend

I am facing a challenge while trying to implement a login function using AngularJS with a Rails backend. I chose to use the $httpBackend for implementation, but I'm encountering an issue. While the $httpBackend function successfully updates the token ...

Customizing Bootstrap Vue to prevent tooltips from opening on hover

I am currently using a tooltip similar to the example shown on Toggle Tooltip: <template> <div class="text-center"> <div> <b-button id="tooltip-button-1" variant="primary">I have a tooltip</b-button> </div& ...

What are the steps for implementing dependencies in a Vue component?

Although I typically work on backend development, I am now faced with the task of implementing something in an existing Vue codebase. In a file named myModal.vue, I need to incorporate a JavaScript library called cron-parser. When I utilize it in the app.j ...

Guide for linking together multiple Axios calls to ensure they execute in sequence and enable each call to access the data obtained from the preceding call

Hey everyone, I have a unique challenge that requires your help. So, I'm working with an array of items and I need to make an Axios post for each item in the array. The catch is that each item relies on data returned from the previous one, so they mus ...

Error: Attempting to execute functions on a dialog before it has been properly initialized with jQuery

Whenever a user clicks on a link, my goal is to display a modal dialog with the response of a GET method fetched using AJAX. The code looks like this: HTML: <a data-rec="@Model.rec" data-seg="@Model.seg" data-det="@Model.seg" class="btn btn-default ch ...

What is the reason for the discrepancy in displaying between these two arrays?

I am facing an issue where I have an array called order containing objects and another array called test that contains a string. With jQuery, I am able to output the contents of test to a <div>, but strangely I cannot do the same for the order array. ...

Initiate an AJAX call and in the event that a particular object is found in the JSON response, proceed to send a subsequent request targeting

My objective is to make an AJAX request to a URL and expect a JSON response consisting of two objects: group_id and next_page. If the next_page object is set, I want to send another AJAX request using the value of next_page as the URL. If there is no next_ ...

What is the best way to retrieve a specific item from an array of objects stored in JSON format using React?

I have received a json file named data.json which contains multiple objects in an array. My goal is to extract the value of a specific key from each object. To achieve this, I am utilizing react redux to fetch these values and present them in a table forma ...

Experience screen sharing through WEBRTC without the need for any additional browser extensions

One question that I have is: Is it possible to implement screen sharing that works on a wide range of devices and browsers without the need for plugins or experimental settings? I have searched online and come across some plugins for Chrome, but I am look ...

Sending multiple AJAX post requests in succession

I'm encountering an issue with my AJAX POST request for a login form. Every time I submit the form, regardless of whether the combination is correct or incorrect, it sends multiple requests Incorrect login: function incorrect() { $('#loginM ...

Guide to utilizing various Nodelists using the forEach function

I'm currently developing an online store project that includes a shopping cart feature. My goal is to send a POST request to the server with the items selected by the user in the cart. I have initialized an empty array and I have 3 Nodelists containin ...

Utilize passed props in components as well as Redux state information

Typically, you can access props sent by a parent to a child component on the child component itself. However, when using Redux in child components, the props sent by the parent are lost due to the use of the 'connect' method which maps the Redux ...

Guide to retrieving objects in React.js

Struggling to extract a country from an online JSON file that I am currently fetching. I am attempting to streamline the process by creating a function to retrieve the country from the dataset and avoid repeating code. However, I am encountering difficulti ...

What is the method to access a variable within an attribute that is not prefixed with ng-*?

For my project, I am utilizing a datePicker called in conjunction with AngularJS. Here is an example of how I incorporate it: <div class='col-md-2'> <div class="form-group"> <label>End date</label> <div clas ...