Step-by-step guide on transferring form data from an Ionic application to parse.com MBaaS through the REST API

Today is my first day with Ionic/Angular, and I'm diving in out of necessity.

I've been using tutorials and documentation to create a demo/test app that submits data to the Parse.com MBaaS service.

However, I seem to be stuck somewhere and clueless on how to add the three form fields.

Here are some details: the app name is TestApp and the class/table name is data.

There are three columns: fname, lname, and uname (for first name, last name, and username).

Below is the code I've been following. I would greatly appreciate any help.

index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>

        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

        <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
        <link href="css/ionic.app.css" rel="stylesheet">
        -->

        <!-- ionic/angularjs js -->
        <script src="lib/ionic/js/ionic.bundle.js"></script>

        <!-- cordova script (this will be a 404 during development) -->
        <script src="cordova.js"></script>

        <!-- your app's js -->
        <script src="js/app.js"></script>
      </head>
      <body ng-app="starter">



    <div>
        <div>
            <ion-nav-bar class="bar-stable">
                <ion-nav-back-button class="button-icon icon ion-ios7-arrow-back">Back</ion-nav-back-button>
            </ion-nav-bar>
            <ion-nav-view></ion-nav-view>
        </div>
    </div>

        <!-- Center content -->
       <ion-view title="Add Data">
        <ion-content padding="true" scroll="false" class="has-header">
            <div class="spacer" style="height: 100px;"></div>
            <form ng-controller="defaultCtrl">
                <ion-list>
                    <label class="item item-input">
                        <span class="input-label">First Name</span>
                        <input type="text" placeholder="First Name here" name="fname" ng-model="starter.fname">
                    </label>
                    <label class="item item-input">
                        <span class="input-label">Last Name</span>
                        <input type="text" placeholder="Surname Here" name="lname" ng-model="starter.lname">
                    </label>
                    <label class="item item-input">
                        <span class="input-label">Username</span>
                        <input type="text" placeholder="Username here" name="uname" ng-model="starter.uname">
                    </label>
                </ion-list>
                <button class="button button-calm button-block" type='submit' ng-click="create(starter)">Add data</button>
            </form>
        </ion-content>
    </ion-view>

    <script type="text/javascript">
    angular.module('starter',['ionic']).factory('starter',['$http','PARSE_CREDENTIALS',function($http,PARSE_CREDENTIALS){
        return {
            create:function(data){
                return $http.post('https://api.parse.com/1/classes/data',data,{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        'Content-Type':'application/json'
                    }
                });
            }
        }
    }]).value('PARSE_CREDENTIALS',{
        APP_ID: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        REST_API_KEY:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    });

    angular.module('starter', ['ionic'])
    .controller("defaultCtrl", function ($scope) {
    $scope.starter = {};
     $scope.create=function(){
            starter.create({fname:$scope.starter.fname}).success(function(data){

            });

    });
    </script>

      </body>
    </html>
    

Answer №1

Ensure authentication headers are set correctly

PARSE_HEADER_CREDENTIALS = {
    "x-parse-application-id": "PARSE-APPLICATION-ID",
    "x-parse-rest-api-key": "PARSE-REST-API-KEY"
};

code

addObject: function (_params) {

    // Set authentication header for POST request
    var settings = {
        headers: PARSE_HEADER_CREDENTIALS,
    };
    // Specify data to add for POST request and convert to string
    var dataObject = {
        "name": _params.name,
        "room": _params.room,
    };

    var dataObjectString = JSON.stringify(dataObject);

    // $http returns a promise with a then function
    return $http.post(baseURL + 'classes/stuff', dataObjectString, settings)
        .then(function (response) {
            // Response contains the result in resp.data
            console.log('addObject', response);
            return response.data;
        });
},

For a complete example, visit the project repository here: https://github.com/aaronksaunders/info-rest-api-ionic-sample

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

Error: Attempting to access the 'body' property of an undefined object following a POST request in a MEAN application

Currently, I am attempting to send POST data from AngularJS to Express. My approach involves using curl to transmit the POST content. After sending the data and receiving a 200 response, I encounter an issue when trying to access the data through body-par ...

Cookies are mysteriously invisible in the developer console of Safari/Chrome when using the Set-Cookie Header, yet they miraculously appear in server logs

Storing cookies for my web app using the 'Set-Cookie' header response from my python backend has been a challenge. https://i.stack.imgur.com/XMx35.png An ajax call on the client-end to the function is where I run into issues: https://i.stack.im ...

Revise Script to Duplicate Alt Attribute onto Miniatures

I'm customizing a gallery plugin for a website that I am currently developing, aiming to add support for titles. The script I am using can be found here: DEMO: http://jquery.malsup.com/cycle/pager2.html CODE: All the functionalities are in place e ...

In a Vue Application, the static HTML elements are loaded before fetching data

Utilizing VueJS and VueRouter in my application has presented a challenge. The issue lies in the fact that static HTML elements within the Vue Component load before the data is fetched, resulting in empty forms and tables being displayed initially. I have ...

Should FormBuilder be utilized in the constructor or is it considered a poor practice?

section, you can find an example of implementation where declarations for formBuilder and services are done within the constructor(). While it is commonly known that using services inside the constructor() is not a recommended practice and should be done ...

What is the solution to the error message "Cannot assign to read only property 'exports' of object '#<Object>' when attempting to add an additional function to module.exports?"

I've been struggling for the past 5 days with this issue. Despite numerous Google searches, I haven't found a solution that works for me. I have a file called utils.js which contains useful functions to assist me. However, when I include the func ...

User interface navigation child components

I am in the process of developing an angularJS application. Each page in my app consists of a header, content section, and footer. The header and footer are consistent across all pages. Currently, I have placed the header and footer directly in the index.h ...

Utilizing Angular and Ionic to load all content through XHR requests

We are currently facing a performance issue with our Angular + Ionic app that is being run through Cordova. Upon investigating in Chrome Dev Tools Network tab, we have identified two main issues: There is duplicate loading of CSS We are seeing XHR reques ...

Learn how to dynamically modify the text and color of a column value within a <v-data-table> component in Vue.js 2.6.11 and Vuetify 2.2.11 based on a specific condition

In my current project where I am developing a web application using ASP.NET CORE for the backend and vue.js for the frontend, I encountered an issue with Vuetify's CRUD Datatable UI Component in a page named "Category". The problem arises when trying ...

Iterate through a JSON object using JavaScript, with distinct keys but the same object structure

Hello, I'm currently in the process of creating a slider using images sourced from a json file. Here is the json structure that I am working with: { "info":[ { "slide1":[ { "title" ...

New and personalized bindings in knockout.js for dynamically updating a dropdown menu based on the selection in another dropdown menu

I have been using knockout for a few months now and have been getting along just fine. However, I recently encountered an issue where I cannot update the options within a SELECT tag because the ajax methods that retrieve data from the server are inside a ...

Using Nestjs to inject providers into new instances of objects created using the "new" keyword

Is it possible to inject a provider into objects created by using the new keyword? For instance: @Injectable() export class SomeService { } export class SomeObject { @Inject() service: SomeService; } let obj = new SomeObject(); When I try this in my t ...

Opening the Gmail app from a link using JavaScript

What is the best way to open the Gmail app from a hyperlink? This link opens WhatsApp <a href="https://wa.me/">whatsapp</a> <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a1f190f ...

Using $index in ng-class to dynamically apply form validation styles

Looking to validate an angular form input and add a class if it's not $valid. The catch is, it's nested inside an ng-repeat looping through an array of integers, with the input name based on the $index: <li ng-repeat="item in selectedItem.dat ...

Are there distinctions between using app.use("/", express.static) versus app.use(express.static)?

Is there a distinction between the following scenarios, assuming we have already executed app.set('thePath', thePath)? app.use('/', express.static(thePath)) app.use(express.static(thePath)) app.use(express.static(app.get('thePath ...

What's the best way to use JavaScript to obtain the width of a 'css-pixel' based on a media query?

While there have been discussions on how to determine device sizes using media queries like Twitter Bootstrap, I am looking for a reliable way to achieve the same output using JavaScript. Specifically, I want to get the CSS media query pixel number rather ...

What is the best way to align text extracted from an API using JavaScript?

I'm currently exploring content generation through APIs, but I'm facing an issue where the text generated is not aligning properly within the container on the screen. The main problem lies in getting the card to be centered on the screen with al ...

Executing a function after the completion of another

Here is my function: function functionName($results) { //do some stuff disableSave() } When this function runs, I want to call the enableSave() function. How can I achieve this? I attempted to pass the function as a callback but I am unsure wher ...

Trouble arises when attempting to bind a JavaScript event with an innerHTML DOM element

I am currently dealing with a complex issue on how to bind a JavaScript event to an innerHTML DOM element. The scenario involves having a div with an input checkbox inside it, along with some pre-existing JavaScript event bound to that checkbox. Additional ...

Transferring a row name from PHP to AJAX using jQuery - the complete guide

In my current project, I have a table that displays details fetched from the database. if(mysql_num_rows($sql) > 0) { $row_count_n = 1; while($rows = mysql_fetch_assoc($sql)) { extract($rows); $options1 = select_data_as_options( ...