Tuesday 15 July 2014

javascript - How to return data from web service in common js module -


I am writing a mobile app using titanium alloy. I do not understand how to write a module that will return the object. In which version I have written "undefined" returns - any help would be appreciated!

How do I consume modules in index.js

  var ResArgs = ("webdata") is required. GetItemDetails (args. Barcode, logic type); Ti.API.info ("Data obtained from WebGetData:" + ResArgs);  

This is the real WebData.js module

  export.GetItemDetails = function (code, type) {var url = "http: // url /" + Code + "/" + type; Var arg = faucet || {}; Var Client = Ti.Network.createHTTPClient ({// function is called when response data is available; Load: function (e) {Ti.API.info ("Text received from service:" + this reply text); Come back.;}, // When an error occurs, a timeout works with terrorism: function (e) {Ti.API.debug (e.error); Warning ('Error:' + E. terror) ;}, Timeout: 5000 // in milliseconds}); Client.open ("GET", URL); // Request Request client.send (); };  

use of a callback

  exports.GetItemDetails = Function (code, type, _callback) {var url = "http: // url /" + code + "/" + type; Var arg = faucet || {}; Var Client = Ti.Network.createHTTPClient ({// function is called when response data is available; Load: function (e) {Ti.API.info ("Text received from service:" + this.responsetext); _callback & Amp;; _callback ({success: true, data: this.responsetext});}, when an error occurs, in which there is a timeout terrorism, the function is called: function (e) {Ti.API.debug (e .error); Alert ('error:' + E. terror); _callback & amp; _callback ({success: false, error: E. terror});}, timeout: 5000 / / in milliseconds}}; Client.open ("GET", URL); // Request Request client.send (); };  

Then you will call this type of code

  var ResArgs = require ("WebData"); ResArgs.GetItemDetails (argsbarcode, args type, function (_response) {if (_response.success) {Ti.API.info ("Data obtained from WebGetData:" + _response.data);} Other {Ti.API error ("Error from WebGetData:" + _response.error);}});  

No comments:

Post a Comment