1

I want to ask you how to call the REST Service from cordova application. I use the MongoDB as stored data. I want to add data from index.html to MongoDB. I can access the url localhost:28017/AlitaDB/test/ from browser and display data from MongoDB. Here the code index.html

index.html

    <!DOCTYPE html>
<html>
<head>
<title>Manage Contact</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.4.8.js"></script>
<script src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $("#submit").click(function insertContact(){
        $.post("http://localhost:28017/AlitaDB/test/",
            $("#insertContact :input").serializeArray(), 
            function(json){
                if(json== null || json == 'undefined')
                    alert("Insert failed");
                else
                    alert("Insert successful");
            });
            alert('asdas');
            return false;
        });

    });
</script>
</head>
<body>
    <h3>Insert Contact</h3>
    <form id="insertContact">
        <table>
            <tr>
                <td>Contact Id</td>
                <td><input type="text" name="contactId" /></td>
            </tr>
            <tr>
                <td>First Name</td>
                <td><input type="text" name="firstName" /></td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><input type="text" name="lastName" /></td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="email" /></td>
            </tr>
            <tr>
                <td><input type="submit" id="submit" name="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

1 Answers1

0

MongoDB requires you to write a RESTful Interface to use their database as they do not provide one, although there are some wrappers/plugins available (Does MongoDB have a native REST interface?). An alternative service is offered by www.Parse.com. I'm not affiliated, but I'm looking to use their schemaless database offering.

AngularJS example code to create a GameScore record with some data:

$http({ 
        url: 'https://api.parse.com/1/classes/GameScore', 
        method: 'POST',
        headers: {
            'X-Parse-Application-Id': 'APPLICATION_ID',
            'X-Parse-REST-API-Key': 'REST_API_KEY',
        },
        data: {"score":1337,"playerName":"Ian Brown","cheatMode":false}
    });

Taken from CURL example at https://www.parse.com/docs/rest#summary

Community
  • 1
  • 1
Zymotik
  • 6,412
  • 3
  • 39
  • 48