0

I have a development set up question.

So I have a Ionic app that I can run locally by simply running: ionic serve

Then in the config for the app I point it at local.mysite.com (which is a wordpress set up). It hits the site when I attempt to login (set up breakpoint in the custom endpoint plugin that is set up in the constructor) but does not hit the function that is the callback for the login.

I console logged which URL $http.get(url).then method is requesting and went to that login in the url and the expected behavior occurred.

I'm mainly just looking for an answer as to if this is possible and if there are any tricks to get this to work.

David Jarrin
  • 1,177
  • 2
  • 15
  • 35

2 Answers2

0

A working code example below with Ionic V1:

In your WordPress API file Add header:

header('Access-Control-Allow-Origin: *');
header( 'Access-Control-Allow-Headers: Authorization, Content-Type' );

Your Factory.js file code:

starter.factory('CommonFactory', function($http) { 
    var base= 'your api file url';
    return {
      //with POST data method        
        login_function: function(user, pass) {
            return $http({
                url: base,
                method: 'POST',
                headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                data: postData,
            });
        },

        // With GET data method
        login_function: function(user, pass) {
            return $http({
                url: base+ '?username='+user+'&password='+pass,
                method: 'GET',
                headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            });
        },

      }
    });

You can changes or rewrite it as per your need.

Controller.js

CommonFactory.login_function($scope.user,$scope.pass)
             .success(function(res) {
                // success handling
                 })
              .error(function(result) {
                //error handling
                 })

Make sure to inject dependency.

Sohan
  • 1,101
  • 1
  • 12
  • 25
0

I went into the error logs for my local env and found the issue to be that I was getting the fatal error from

PHP Fatal error: uncaught error: call to undefined function utf8_encode()

Then found this issue could be solved by installing php7.1-xml like so

sudo apt-get install php7.0-xml
sudo service apache2 restart

(thanks to this post utf8_(en|de)code removed from php7?)

David Jarrin
  • 1,177
  • 2
  • 15
  • 35