0

I have created a service and I using that for my login:

EDIT: Added the 'success' and 'error' code.

EDIT 2: I am developing an iOS mobile application which includes Javascript/AngularJS. So is there a way to view errors as alerts..

.service('Login', function($http, $q, $httpParamSerializerJQLike) {
   return {
      loginUser: function(ipAdd, name, pw) {

         var sendurl = 'http://' + ipAdd + ':8080/loginuser/user.cgi';

         var postData = {
            'ACTION' : 'login',
            'LOGIN' : name,
            'PASSWORD' : pw
         }; 

        //what is the mistake here?
         return $http({
                      method : 'POST',
                      url : sendurl,
                      data : $httpParamSerializerJQLike(postData),
                      headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
                      }).success(function(response) {
                         var x2js = new X2JS();
                         var jsonObj = x2js.xml_str2json(response.data);

                         if (typeof jsonObj === 'object') {
                             alert("here:1");
                             return jsonObj;
                         } else {
                             alert("here:2");
                             // invalid response
                             return $q.reject(jsonObj);
                         }

                      }).error(function(response) {  
                         //do error
                         //comes here when no internet connection is found..
                           alert("here:3");
                           return $q.reject(response.data);
                      });

         }
      }
   })

I have also included this in app.js:

var app = angular.module("myApp", ['starter.services'],function($httpProvider){
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
});

My actual url looks like this:

'http://' + ipAdd + ':8080/loginuser/user.cgi?ACTION=login&LOGIN=' + name + '&PASSWORD=' + pw;

I have tried this approach too: https://stackoverflow.com/a/25570077/5876598

My service is not returning anything.. I want to know if I'm doing mistake in my url formation, or while sending data.

Thanks.

Community
  • 1
  • 1
Swift
  • 397
  • 1
  • 2
  • 11

3 Answers3

0

The best way to know where the mistake comes from is to check the 'network' tab on your navigator developer console.

Assuming you are runing on linux or mac, you can also try to use CURL to have an idea of what return the url you are trying to reach.

We can't help you with the code only.

Dlacreme
  • 196
  • 1
  • 8
0

I added a deferred promise in my service.

I also changed "success().error()" to ".then(function(data, status, headers, config){}) . Don't know why it didn't work when I used success and error.

Swift
  • 397
  • 1
  • 2
  • 11
0

Actually previously I noticed some issue while rendering promise itself in service. Follow the below structure

.service('Login', function($http, $q, $httpParamSerializerJQLike) {
  return {
   loginUser: function(ipAdd, name, pw) {

     var sendurl = 'http://' + ipAdd + ':8080/loginuser/user.cgi';

     var postData = {
        'ACTION' : 'login',
        'LOGIN' : name,
        'PASSWORD' : pw
     }; 

    //what is the mistake here?
     return $http({
                  method : 'POST',
                  url : sendurl,
                  data : $httpParamSerializerJQLike(postData),
                  headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
                  });

     }
  }
})

.controller('SomeCtrlName',function(Login){
    //pass your parameter to below service.
    Login.loginUser().then(function success(){
    //write success code here 
   },function error(){
    //write error code here
   )};
})