angularjs - Response string JavaScript undefined -
i have program written in angularjs. i'm receiving json data server when online. i'm developing offline mode now..
i have problem here dont know why cant fix.
i saved json info localstorage when program offline json string.
service.js - webservicecall
webservicecallpost: function(data, action) { console.log("data "+json.stringify(data)); console.log("action "+json.stringify(action)); var deferred = $q.defer(); if (navigator.connection.type != "none") { return $.ajax({ type: "post", url: appconst.serviceurl.service + action, crossdomain: true, datatype: "json", data: data, timeout: 2000000, async: true, success: function(response) { localstorage.setitem(data + action, json.stringify(response)); deferred.resolve(); }, error: function(xhr, ajaxoptions, thrownerror) { $ionicloading.hide(); if (xhr.status == 0) { window.plugins.toast.showshortbottom($translate.instant("timedouterror")); } else if (xhr.status == 404) { window.plugins.toast.showshortbottom($translate.instant("timedouterror")); } else { window.plugins.toast.showshortbottom($translate.instant("timedouterror")); } }, beforesend: function() {}, complete: function() {} }); } else { window.plugins.toast.showshortbottom($translate.instant("checknetworkconnection")); $ionicloading.hide(); var response1 = json.parse(json.stringify(localstorage.getitem(data + action))); return $http.get('').then(function(response) { return response1; }); } }
controller.js - retriveing response.
services.webservicecallpost('', appconst.services.get_menu_card).then(function(response) { $ionicloading.hide(); console.log("response: " + json.stringify(response)); if (response[1].response.status == 1) { if (response[0].data.menu.length > 0) { var categoryresponse = []; angular.foreach(response[0].data.menu, function(value, key) { if (value.menu_image_name != '') { var extradata = { imageurl: appconst.serviceurl.menu_image_url + value.menu_image_name } } else { var extradata = { imageurl: 'img/screen.png' }; } angular.extend(value, extradata); categoryresponse.push(value); }); $rootscope.categories = globalmethods.getdashboardgridview(categoryresponse, 2); } if (response[0].data.addons.length > 0) { $rootscope.totaladdons = []; angular.foreach(response[0].data.addons, function(value, key) { var extradata = { "finalcost": value.price, "quantity": 1, imageurl: appconst.serviceurl.addon_image_url + value.addon_image }; angular.extend(value, extradata); $rootscope.totaladdons.push(value); }); } $scope.getsitesettings(); } $rootscope.dashboardhistoryid = $ionichistory.currenthistoryid(); });
console output : when check json pretty print looking same.
online response : https://codepaste.net/op0boq
cached response : https://codepaste.net/y3bkd6
problem: typeerror: cannot read property 'status' of undefined
when want response1.response.status ok getting. when i'm offline , cachedresponse1.response.status retriving status undefined. same data, why ?
if code
var cachedresponse = json.parse(json.stringify(localstorage.getitem('' + appconst.services.get_menu_card)));
uses asynchronous call
console.log("cached response: " + cachedresponse);
won't wait finish , print undefined
Comments
Post a Comment