ionic framework bad request when calling web service over 3G - ios

I'm developing iOS app using ionic framework and I have one problem when I try to call web service by using 3G network.
here is my service in UserService:
function getUserStat(user_id){
var request = $http({ method: "get",
url: "http://www.example.com/user.php",
params: {
action: "stat",
user_id:user_id
},
data: {
}
});
return(request.then(handleSuccess, handleError));
}
function handleError( response ) {
// The API response from the server should be returned in a
// nomralized format. However, if the request was not handled by the
// server (or what not handles properly - ex. server error), then we
// may have to normalize it on our end, as best we can.
if (!angular.isObject( response.data ) || !response.data.message) {
return( $q.reject("An unknown error occurred.") );
}
// Otherwise, use expected error message.
return( $q.reject( response.data.message ) );
}
// I transform the successful response, unwrapping the application data
// from the API response payload.
function handleSuccess( response ) {
return( response.data );
}
the getUserStat() function will return json back.
here is my controller
UserService.getUserStat($scope.user_id).then(function(data){
alert("Result: " + JSON.stringify(data));
});
in my control I just show the json.
I build this code to my iPhone and test it over WIFI network, everything work fine. If i update the serverside, UserService.getUserStat in controller will show update. but the problem is when I test it on 3G network, iPhone always show the old json returned from the server (even I change server side data).
any idea to solve this problem?
Thank you

I had a similar problem when I tried to upload a camera photo to my data server.when i tested the app on my local WIFI it worked perfectly but when I tested it outside i noticed it fails to upload the file. eventualy the problem was that since the internet outside is much slower the app moved to another view without finish the upload action.
so for example if your controller looks something like this:
.controller('Ctrl1', function(webService, $scope, $state) {
UserService.getUserStat($scope.user_id).then(function(data){
alert("Result: " + JSON.stringify(data));
});
$state.go('app.posts');
});
it should be like this:
.controller('Ctrl1', function(webService, $scope, $state) {
UserService.getUserStat($scope.user_id).then(function(data){
alert("Result: " + JSON.stringify(data));
})
.finally(function() {
$state.go('app.posts');
});
});

Related

Vue.JS Axios POST request .then method

Im stuck!
I'm sending a POST request to a server via AXIOS from a VUE app.
Everything works and the server responds.
The problem is, that the .then method never gets called. How could that be?
Bonus question how are you doing your requests from VUE?
Many thanks
methods:{
customMethod1: function() {
//AXIOS
var config = {
headers: {'My Custom Header 1': 'Header-Value'}
};
//POST request
axios.post('http://192.168.56.101:5000/post1', {name: 'Dave'}, config)
.then(function(response){
alert("posted successfully");
});
},
}
Have you confirmed via a tool like Fiddler that the server actually sends a response? It could be that the server never actually responds.
I just found the solution, and I don't know if I should share it with the person who posted this question : he could at least answer to the others trying to help him.
As I find many answers here I post my solution (maybe not the correct answer but it works)
It's not a vue.js issue, nor axios, the .then statement is called only when you return something on server side.
Some code to explain :
Product.update(
{
productName: req.body.productName,
productDesc: req.body.productDesc,
productOwner: 0,
productImage: req.body.productImage,
productState: req.body.ProductState,
productPrice: req.body.ProductPrice
},
{
where: {
id: req.body.id
}
})
.then(function (item) {
console.log("Product update " + item.id);
res.send({id: item.id});
})
.catch(function (err) {
console.log("Product update error " + err);
});
the important line here is :
res.send({id: item.id});
If you return nothing at server side your .then at client side is not called.
let me know if it works but +1 me ;-)

Ionic Angular. $http and geolocation.getCurrentPosition do not appear to work in iOS simulator

I'm developing a simple iOS app using Ionic.
I have 2 bits of functionality that do not appear to work in the iOS simulator that are working fine when I run the app in chrome.
Firstly, I am using the $http service in a factory to pull in some data:
var harbours = function () {
var deferred = $q.defer();
$http({
method: 'GET',
url: '../data/harbourData.json'
}).success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function(data, status, headers, config) {
deferred.reject(status);
});
return deferred.promise;
}
.....
return {
all: harbours
....
}
and then set it in my scope in the controller with:
var harboursPromise = Harbours.all();
harboursPromise.then(function(response){
console.log('response from controller', response);
$scope.harbours = response.harbours;
});
This does not appear to execute as I do not get the data in my template, whereas I do when running the app in chrome.
Secondly, geolocation. This piece of code is working fine in the browser but not when I build and run the app in the simulator:
$scope.position = 'Trying to find your location...'
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
$scope.$apply(function(){
$scope.position = position.coords.latitude+", "+position.coords.longitude;
})
})
} else {
$scope.position = "Sorry, we can't get your location";
}
In the running app, it appears to have access to navigator.geolocation as $scope.position does not get changed to "Sorry, we can't get your location". I'm sure there is something fundamentally wrong that I am / am not doing. Can anyone point me in the right direction, please?
Thank you.

Stream video to an ios app from a mongoDB using Gridfs-stream

I'm a relatively new programmer trying to create an ios video-streaming app in swift. I've written the backend in node.js and have a mongoose connection to a mongoDB. I've been able to upload videos to the database using the following code and gridfs
function (req, res, next) {
req.pipe(gfs.createWriteStream({ filename: 'filename'}));
res.send("success");
};
I'm attempting to stream the videos using the following code
gfs.findOne({_id: req.params.id}, function (err, file) {
if (err) {
return res.status(400).send(err);
} else if (!file) {
return res.status(404).send(' ');
} else {
res.header("Content-Type","video/mp4");
res.header("X-Content-Type-Options", "nosniff");
res.header("Accept-Ranges", "bytes");
res.header("Content-Length",file.length);
var readStream = gfs.createReadStream({_id: file._id});
readStream.on('open', function () {
console.log('Starting download...');
});
readStream.on('data', function (chunk) {
console.log('Loading...');
});
readStream.on('end', function () {
console.log('Video is ready to play');
});
readStream.on('error', function (err) {
console.log('There was an error with the download' + err);
res.end();
});
readStream.pipe(res);
}
});
When I run a server on localhost and attempt to access the videos via google chrome, all I get is the default playback screen but no video. However, ultimately I am trying to playback on an ios app. When I try the same thing on swift (using the local host url passed in to mpmovieplayer), there is also no video playback. I know the get request is going through because my console outputs the proper response with the filesize. I have even been fiddling with alamofire on the front end and can see the hexadecimal representation of the video in the response data. Can anybody help with this code? Do I need to update my res.header to fit some IOS specification? Should I even be using alamofire at all for this? Thanks in advance for your responses.

$http error handling in AngularJS

$http in my AngularJS project not able to recognize 40X(401,403,405...) errors on iOS.
I am using 1.2.10 AngularJS version and Cordova version 3.4.0.
Below is the code I am using:
TE_SERVICES.factory('hello',function ($http,$rootScope) {
return {
loginUser: function(userCredentials,successCallback,errorCallback){
$http({
method: "GET",
url: "data/example.json",
headers: {"Authorization":'Basic '+userCredentials},
}).then(function(response){
successCallback(response.data);
console.log("Success------"+JSON.stringify(response))
},function(data, status, headers, config){
errorCallback(data);
console.log("Error------"+JSON.stringify(data)+" "+status)
})
}
}
});
hello.loginUser($rootScope.encodedUserCredencials,function(persons) {
// success handler
}, function(data) {
// error handler
console.log(data.status+"===="+status)
});
data.status is returning 0 and status returns undefined.
Please help me to resolve this issue.
Tried to include the domain in whitelist on IOS.But no solution :( It still gives the same response.
But the same code works absolutely fine in Android.
Please help me to resolve this issue.
Thanks in advance :)
So you r using the $http from angular. Do you use the error callback or the second function in the then callback ?
Example
$http.get("someUrl")
.success(function(response){}) // if http code == 200
.error(function(response){}) // else
Or with then, that can take 2 functions. The first is the onSuccess, the second the onError function.
$http.get("someUrl")
.then(function(response){
// if http code == 200
},
function(response){
// else
});
The response parameter does also contain the error codes.
Consider using a $httpInterceptor to handle all errorcodes at the same place, instead handling them in every http callback.
UPDATE:
It seems, that the angular doc is incomplete/wrong for the success callback.
It doesnt pass 4 parameter there. It does pass a response object that contains all the information about request+response and the passed data.
Update to the edit:
Dont write callbacks by yourself. Use angular promises:
TE_SERVICES.factory('hello',function ($http,$rootScope) {
return {
loginUser: function(userCredentials){
return $http({
method: "GET",
url: "data/example.json",
headers: {"Authorization":'Basic '+userCredentials},
}).then(function(response){
return response.data;
},function(response){
return response;
});
}
}
});
hello.loginUser($rootScope.encodedUserCredencials)
.then(function(persons) { // success handler
}, function(data) { // error handler
console.log(data);
});
Try this and tell me if the console.log logs something.
I had exactly the same problem. Cordova app, angular js, IPhone and 401 requests are not received in angular js http interceptor. They work fine on android devices.
My finding was that IPhone browser is handling those at a higher lever and trying to use WWW-Authenticate information to do authentication. This is why the response does not get to angular.
The only solution I found, was to change my service to return 400 instead of 401 in case of an api request. In this case I return 400 with an error message that I handle on client side.
I hope this helps.
My issue with the 403 status code was that my backend returned a response with status 403 but the body of a response did not contain a JSON string. It contained just a string - Authentication Failed.
The rejection variable was an object Error.
I encoded the body and the rejection variable contains a valid response error object.
To handle HTTP errors I use interceptors.
$httpProvider.interceptors.push(function($q, $location, redirect, HTTP_CODES) {
return {
'responseError': function(rejection) {
if (rejection.status === HTTP_CODES.FORBIDDEN) {
redirect('/login', $location.url());
}
return $q.reject(rejection);
}
};
});

Trigger.io PHP session expires

folks!
I'm trying to run an HTML app in Trigger.io. This app calls ajax to load some data from a PHP page. At this moment, I authenticate the user and start the session. After this, I have to call another PHP page. So, I check for the session started, and I found that the session is not active anymore. The second call is made right after the first one.
This happens when I try to run the app from the Trigger.io ToolKit, using iOS Simulator ( I'm using a Mac - OS X Mountain Lion ). When I test the same app in the Safari, it works perfectly: my PHP server recognizes the session started earlier, and the second page is loaded by ajax.
Is there any parameter I have to set? Or Trigger.io does not support PHP sessions?
Thank you.
Marcio
You should use forge.request.ajax(params) to make ajax requests, this is because of cross domain restrictions, as Forge apps are loaded as file:// urls on iOS.
forge.ajax.request takes a similar input to jQuery's $.ajax so you can easily switch between them if you want to use the same code on a website as well as in your Forge app.
More documentation is available in our docs: http://docs.trigger.io/en/v1.4/modules/request.html#ajax
#Connorhd , thank you so much for helping!
First of all, I do an ajax request to "app_authUser.php" to authenticate user. Then, if Ok, I do another ajax request to "app_loadFeed.php" to load data from server.
In the first ajax request, I pass, in the post parameters, the user data ( login and password ) to authenticate the user on database. The first command I call on the php file "app_authUser.php" is "session_start()", as follow:
CODE FOR "app_authUser.php" ON THE SERVER SIDE:
<?php
session_start();
// Just for test;
$idSession = session_id();
echo $idSession;
/*
Test user authenticate. If ok, then I assign idUser to $_SESSION['idUser'] variable.
*/
if( $loginOk == true ){
$_SESSION[ 'idUser' ] = $User->idUser;
}
?>
In the code above, the user is authenticated and $_SESSION['idUser'] is correctly initiated. In the second ajax request, I test if the variable $_SESSION[ 'idUser' ] is set. That's the point: it isn't set anymore. Again, I call "session_start()" at first:
CODE FOR "app_loadFeed.php" ON THE SERVER SIDE:
<?php
session_start();
// Just for test;
$idSession = session_id();
echo $idSession;
if( isset( $_SESSION[ 'idUser' ] ) ){
/* Load data from database to return to user.... */
echo ...
}
else{
echo '0';
}
?>
The test above always return false when I call my app from Trigger.io compiler. But, when I call my app from Safari, it always returns true.
I found out that the function "session_id()" returns different values in both ajax requests when the call is made from app running on Trigger.io compiler, but the same value when app is running on Sarari:
Example:
Requests from Trigger.io:
First request: echo $idSession returns "m7dbsv7qqem92os39lv5ao2ta1"
Second request: echo $idSession returns "h49pble06n7ao9pum06kt4dph0"
Requests from Safari:
First request: echo $idSession returns "2cbhin1185fm5ehvbb15k6n0b1"
Second request: echo $idSession returns "2cbhin1185fm5ehvbb15k6n0b1"
That means the session isn't the same at the first example. Why does this happen?
I'm not using forge.request.ajax in my app. That's my ajax code, in my javascript:
<script>
function openAjax()
{
try
{
var ajax = new XMLHttpRequest();
}
catch(e)
{
try
{
var ajax = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(ee)
{
try
{
var ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(eee)
{
var ajax = false;
alert("Seu navegador não suporta AJAX!")
}
}
}
return ajax;
}
// This is the function called at second time, just after I authenticate my user and have session opened.
function loadFeed(){
var url = CT_URLBase + 'app_loadFeed.php'; // CT_URLBase = 'http://192.168.1.100/' in my local environment.
var parameters = '';
var ajax = openAjax();
ajax.open('POST', url, true);
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
ajax.onreadystatechange = function(){
if (ajax.readyState == 4){
if (ajax.status == 200){
var retorno = ajax.responseText;
if( retorno != '0' ){
/* Return OK!!! */
}
else{
/* Error: return is 0 (zero), that means session is not started.
}
}
}
}
ajax.send(parameters);
}
</script>
What most makes me in a misunderstanding is that the same code works perfectly in Safari, but not in Trigger.io.
By the way, I visited your website, connorhd.co.uk ! Great job! It's a great place to find "Interesting stuff"! Congratulations!
Thank you so much for your help!!!
Marcio Clume

Resources