4

I am trying to request to HTTPS server via the post method in Ionic app. But every time, I am failing. I've tried with 2 ways. One way is using $http, and the other way is using $.ajax. But still not found the correct way. The codes are following as below.

-Using $http:

$http({
    url: "https://beta.test.com/auth/authenticate/",
    method: "POST",
    data: {
        "userName": "vv9@test.com",
        "password": "vv9",
        "ipAddress": "2d:5d:3s:1s:3w",
        "remeberMe": true
    },
    headers: {
        'Content-Type': 'application/json'
    }
}).success(function(res) {
    $scope.persons = res; // assign  $scope.persons here as promise is resolved here
}).error(function(res) {
    $scope.status = res;
});

-Using $.ajax:

$.ajax({
    url: "https://beta.test.com/auth/authenticate/",
    type: "POST",
    data: {
        "userName": "vv9@test.com",
        "password": "vv9",
        "ipAddress": "2d:5d:3s:1s:3w",
        "remeberMe": true
    },
    headers: {
        'Content-Type': 'application/json'
    }
}).done(function() {
    console.log('Stripe loaded');
}).fail(function() {
    console.log('Stripe not loaded');
}).always(function() {
    console.log('Tried to load Stripe');
});

How can solve this problem? What's wrong in the codes?

Ved
  • 2,701
  • 2
  • 22
  • 30
Michael Lee
  • 135
  • 1
  • 11

1 Answers1

1

Here is something to get you started, I will update if you provide some plunker or more code.

(function() {
  'use strict';

  angular
    .module('example.app', [])
    .controller('ExampleController', ExampleController)
    .service('exampleServce', exampleServce);

  function ExampleController(exampleService) {
    var vm = this;

    vm.update = function(person, index) {
      exampleService.updatePeople(person).then(function(response) {
        vm.persons = response;
      }, function(reason) {
        console.log(reason);
      });
    };
  }


  // good practice to use uppercase variable for URL, to denote constant.    
  //this part should be done in a service

  function exampleService($http) {
    var URL = 'https://beta.test.com/auth/authenticate/',
      data = {
        "userName": "vv9@test.com",
        "password": "vv9",
        "ipAddress": "2d:5d:3s:1s:3w",
        "remeberMe": true
      },
      service = {
        updatePeople: updatePeople
      };

    return service;

    function updatePeople(person) {
      //person would be update of person.
      return $http
        .post(URL, data)
        .then(function(response) {
          return response.data;
        }, function(response) {
          return response;
        });
    }
  }
})();
alphapilgrim
  • 3,761
  • 8
  • 29
  • 58