1

I'm trying to send a POST request to a rest API, but I don't seem to able to do it correctly, I don't know if it's the syntax, or what it could be, I've tried many different ways but nothing seems to work. Here's is some pictures of the service being consumed by POSTMAN (everything works fine here).

  1. The Headers enter image description here

  2. And the body with the responseenter image description here

But then when I try to consume the service via AngularJS, it keeps sending me erros messages, the localhost server even detects that there is a request, because I can see it on the Eclipse's console, but it doesn't run through the debugger with togglebreakpoints and it does with POSTMAN.

This is one of the many attempts to send a $http request

    var json = { practiceId: 1 };

    $http({
        method: "POST",
        url: "https://localhost/GetAppointmentTypes",
        data: JSON.stringify(json),
        headers: {
          "Content-Type": "application/json",
          "AERONA-AUTH-TOKEN": $window.localStorage['token']
        }
    })
    .success(function(data) {
      $ionicPopup.alert({
            title: 'WORKING'
      });
    })
    .error(function (errResponse, status) {
      if(status == 403){
            $ionicPopup.alert({
                title: '403'
        });
      }
    });

But then enter image description here

And here is another attempt

  var url = 'https://localhost/GetAppointmentTypes';

  var parameter = JSON.stringify({practiceId:1});

  var headers = {
    "Content-Type": "application/json",
    "AERONA-AUTH-TOKEN": $window.localStorage['token']
  };


  $http.post(url, parameter, headers).then(function(response){
      $ionicPopup.alert({
          title: 'WORKING'
      });
  }, function(response){
    $ionicPopup.alert({
        title: ' NOT WORKING '
    });
  });

And the response enter image description here

Note: the $window.localStorage['token'] is working correctly. I have tried so many things I don't even know what I'm doing wrong now, might be a syntax error, but then ERROR 403 wouldn't be showing.

EDITED (ADDED)

And the Response Variable enter image description here

3
  • what is inside response ? Commented May 16, 2017 at 12:07
  • can you post API file data ? if you are using custom API, add header header('Access-Control-Allow-Origin: *'); header( 'Access-Control-Allow-Headers: Authorization, Content-Type' ); in your API file Commented May 16, 2017 at 12:10
  • I'm editing the question with what's in the response variable, sorry if it's confusing, I'm running the app on a emulator and I debug through alerts, maybe not the best way to do it. Commented May 16, 2017 at 12:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.