How include HTTP request in app.run() in ionic - javascript

I'm made android project with ionic V1. I want make app send some function before app close with Cordova event. I try use pause to send data to server but I have stuck in sending data in app.run(). This my code in app.js
.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {
document.addEventListener("pause", onPause, false);
});
function onPause($http) {
$http.get('https://localhost:88/web.php?tN=off&f12=123456789')
.then(function(response){
console.log(response);
}, function(error){
//there was an error fetching from the server
});
}
});
And this error from debugger
Uncaught TypeError: Cannot read property 'get' of undefined
I don't know where what wrong with this code. I used in controller.js it work correctly but in app.js it doesn't work. Please help me to solved this problem. Thanks

$http is missing is your run() parameters

Related

Angular js How to handle HTTP error globally

HI all i have 2 question
angular try to handle server $http error globally. so written below code in app.js
angular.module('mname',[])
.config(function ($provide, $httpProvider) {
$provide.factory('ErrorInterceptor', function ($q) {
return {
responseError: function(rejection) {
//console.log(JSON.stringify(rejection));
alert('Error:==>'+rejection.status+"===>"+rejection.statusText);
return $q.reject(rejection);
}
};
});
1) Here I can able to get that alert if 404 error.
But if server through any code error like missing variable or etc... but connection state is 200 .. that time how can i manage??
can't come inside this responseError: area .
2) I wrote one method for dialog factory to display the errors and message as Factory. I can able to include inside the controller to get working. How can use inside "responseError:" area . (refer above code)

HTTPS 'GET' in AngularJS

I am working on a mobile application using Cordova and AngularJS. I am struggling to run an HTTPS 'Get' call to an Amazon S3 server using the AngularJS $http service. The call fails with:
SSLHandshake: Remote host closed connection during handshake
This is how I make my call to the S3 server:
$http({method: 'GET' , url: path});
The path variable is an HTTPS url to S3. This works fine if the URL is not HTTPS.
I also had this issue when working with the Cordova FileTransfer class, however I was able to solve it by setting trustAllHosts to true when starting the download. This cannot be done with the AngularJS $http service.
Does anybody know how I may solve this? Any help would be greatly appreciated.
In AngularJS you should use $sce.trustAsResourceUrl(url); to use secure URLs like HTTPS.
Example Code:
$scope.trustSrc = function (path) {
if (path) {
if (path.indexOf('https') === -1) {
path = path.replace('http', 'https');
}
}
return $sce.trustAsResourceUrl(path);
};
This should return a secured path that could be used in your GET request like this:
$scope.trustedPath = $scope.trustSrc(path);
$http({method: 'GET' , url: trustedPath});
getUser().then(function (response) {
$scope.users = response.data;
}).catch(function (err) {
debugger;
})
function getUser() {
return $http.get("https://jsonplaceholder.typicode.com/users")
}

Trying to use Twilio with Meteor, ReferenceError: Twilio is not defined

To preface this, I'm very new to Meteor and have never used Twilio before, so I'm probably just making a silly mistake somewhere.
I'm using the Twilio API bindings found here and trying to get a simple snippet of code working to send an SMS message within a Meteor.methods function. Here's the event trigger and method function:
if (Meteor.isClient) {
Template.twilioPlayground.events({
"click button": function() {
Meteor.call("sendSMS");
}
});
}
Meteor.methods({
sendSMS: function () {
twilio = Twilio('i put my account sid here', 'and my auth token here');
twilio.sendSms({
to:'+7199634882',
from: '+17194530451',
body: 'This is a test'
}, function(err, responseData) { //this function is executed when a response is received from Twilio
if (!err) {
console.log(responseData.from); // outputs "+14506667788"
console.log(responseData.body); // outputs "word to your mother."
}
});
}
});
So when that event is triggered, I'm getting the following error:
ReferenceError: Twilio is not defined
at Meteor.methods.sendSMS (http://localhost:3000/myTodoApp.js?8ae55884eab4c6a28ef9da8344fcf0b9d15c24ac:194:18)
at http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4239:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?61916b1060b33931a21f104fbffb67c2f3d493c5:945:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4230:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?1f971b2ac9f4bdab7372cb5098ed1e26ff98dfb2:4108:17)
at Object.Template.twilioPlayground.events.click button (http://localhost:3000/myTodoApp.js?8ae55884eab4c6a28ef9da8344fcf0b9d15c24ac:106:20)
at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:3103:18)
at http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2371:30
at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2029:12)
at null.<anonymous> (http://localhost:3000/packages/blaze.js?77c0809654ee3a10dcd5a4f961fb1437e7957d33:2370:26)
Aside from adding the mrt:moment and mrt:twilio-meteor packages to the project, I didn't do any more setup. Any help is greatly appreciated.
You defined your method on both client and server. But the Twilio symbol is not even exposed on the client (because it is something client doesn't need to know about). Hence you get this error. Put your sendSMS method definition into Meteor.isServer block and it should work fine.

error when reading json file using angular resource

I'm trying to use angular ngResource module to fetch data from json file but I get an error 404 on the console - the URL is being concatenated with code following the call to get function.
Here is the error:
localhost prefix/test_data/data.json/function%20(data)%20%7B%20%20%20%20%20%20%20%20%20%20%20%20console.log(data);%20%20%20%20%20%20%20%20%7D
here is the code I'm using:
the json service -
angular.module('jsonServices',['ngResource']).factory('JsonService',
function ($resource) {
return $resource('/test_data/data.json');
});
the controller using the service:
angular.module('myApp.controllers')
.controller('mainController', function ($scope, JsonService) {
JsonService.get(function (data) {
console.log(data);
});
});
the app js that starts it all:
angular.module('myApp',['myApp.controllers','jsonServices','ui.router'])
.config(function($stateProvider, $urlRouterProvider){
$stateProvider.state('home',{
url:'/home',
template:'some template path goes here',
controller:'mainController'
};
});
As you can see the "function(data)" is being added to the url as a string. one more thing is when I try to access the json file browsing to its location I can see the content of the json file OK.
Does anyone got this problem before and find a way to solve it? Is there something I'm doing wrong or missing here?
Thanks,
Eran
Found the problem, it seems that I used a bower package call ng-resource instead of angular-resource.

Karma unit test cross domain resource AngularJS

I use karma as my angular project test running framework, my angular have server service need to access another web url to get data like http://localhost:8081/common/countries get all information about country.
my problem is my karma start at localhost:9876 and it need to get data from http://localhost:8081/common/countries this cause cross domain problem by the browse same-origin policy.
so I get below error in my console:
Error: Unexpected request: GET http://localhost:8081/common/countries
No more request expected
at Error (<anonymous>)
at $httpBackend (http://localhost:9876/absoluteC:/WebUI/WebUI/test/lib/angular/angular-mocks.js:934:9)
at sendReq (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:9087:9)
at $http (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:8878:17)
at Object.getMock (http://localhost:9876/base/share/services.js:644:17)
at Object.get (http://localhost:9876/base/share/services.js:347:28)
at Object.getCountries (http://localhost:9876/base/share/services.js:221:22)
at Object.clSingleSelectConfig.nationality.getData (http://localhost:9876/base/share/directives.js:146:32)
at http://localhost:9876/base/share/directives.js:192:44
at nodeLinkFn (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:4360:13)
What i have tried:
1 install karma plugin karma-chrome-launcher and add --disable-web-security in my config file. but it doesn't work.
2 set'Access-Control-Allow-Origin''Access-Control-Allow-Headers''Access-Control-Allow-Methods' in header to allow origin access in server response.
all above don't work, so how to solve my problem?
For cross domain requests use expectJSONP and be sure to use a callback parameter.
describe('stackoverflow.activity tests', function () {
var svc, httpBackend;
beforeEach(function (){
module('ngResource');
module('stackoverflow.activity');
inject(function($httpBackend, StackoverflowActivityService) {
svc = StackoverflowActivityService;
httpBackend = $httpBackend;
});
});
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should send the message and return the response', function (){
var returnData = { testing: 'anything'};
httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK').respond(returnData);
svc.events({
user:'gigablox',
params:{
callback:'JSON_CALLBACK'
}
}).get(function(user) {
expect(user.testing).toEqual('anything');
});
httpBackend.flush();
});
});
Source for this example

Categories