Sunday, July 13, 2014

titanium - how to send data to Parse.com thru REST API (insert)

var url = "https://api.parse.com/1/classes/Hello";

 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: \n" + this.responseText);
     // returns a json string. eg.: {"createdAt":"2014-07-13T15:14:22.467Z","objectId":"JKWFuRnf0T"}
         var response = JSON.parse(this.responseText);
         Ti.API.info("Received object: \n" + response);
     
         activityIndicator.hide();
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         textarea.value = e.error;
         activityIndicator.hide();
       
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 client.open("POST", url);
 // copied from Parse online settings > Application keys
 client.setRequestHeader("X-Parse-Application-Id", "/**** copied from parse.com ****/");
 // copied from Parse online settings > Application keys
  client.setRequestHeader("X-Parse-REST-API-Key", "/**** copied from parse.com ****/");
   client.setRequestHeader("Content-Type", "application/json");
   // data to insert
   var data = {
           title:'lord of the ring',
           price:15.99,
           description:'frodo saves the middle-earth'
   };
 // Send the request.
 client.send(JSON.stringify(data));

No comments: