jsontitaniumtitanium-alloyapigee-baas

how to update existing json object in java script with titanium studio


var args = arguments[0] || {};
$.atn.text=args.attendance;
Ti.API.info('attendance:'+args.attendance);
function doClick(e){
  $.atn.value=$.atn.value+1;
  Ti.API.info('atn is'+$.atn.value);

  var url = "api.usergrid.com/PRI_95616/LOGIN/attendances?";

  var client = Ti.Network.createHTTPClient({
    onload : function(e) {},

    onerror : function(e) {
      Ti.API.debug(e.error);
      alert('error');
    },
    timeout : 5000  // in milliseconds
  });
  client.setRequestHeader('content-type', 'JSON');
  client.open("PUT", url);

  client.send(JSON.stringify(jsonobject));
}

I want to fetch and then update the attendance value then insert the updated value in database. How can I do it?


Solution

  • If by "update the database" you mean PUT to the Restful API, then you have one small error.

    client.send(JSON.stringify(jsonobject));
    

    jsonobject is not defined. It must be the json (JavaScript) object you just built. if $.atn is the object you're pushing values into then try:

    client.send(JSON.stringify($.atn));
    

    I don't know the REST API specs for usergrid.com, but if all you have to do is PUT a json object with .text and .value defined, the URL "api.usergrid.com/PRI_95616/LOGIN/attendances?" then this should do it. However, you'll have to put http:// before the URL like:

    "http://api.usergrid.com/PRI_95616/LOGIN/attendances?"