lagfvu
lagfvu

Reputation: 597

ionic api call to laravel returns connection error

Below is my angular service, i know it works because i tried it out on a node server, but it's not working with my laravel API.

angular.module('starter')

  .service('AuthService', function($q, $http, API_ENDPOINT) {

    var register = function(user) {
      return $q(function(resolve, reject) {
        $http.post(API_ENDPOINT.url + '/signup').then(function(result) {
          if (result.data.success) {
            resolve(result.data.msg);
          } else {
            reject(result.data.msg);
          }
        });
      });
    };


    return {
      register: register
    };
  });

Below is my laravel code

Route::group(['prefix'=>'api', 'middleware' => 'cors'], function()
{

    Route::post('/signup','UserController@index');
});

Below here is my cors middleware

public function handle($request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', '*');
}}

Finally, this is my controller index function which simply returns a json response

  public function index()
    {
        return response()->json(['success' => 'Received','Done'=>'It works']);
    }

I have used postman to test it out and i get the response as expected, but when i call it from my ionic app i get a connection refused error in my developer tools console.

Upvotes: 0

Views: 606

Answers (1)

Luis Caballero
Luis Caballero

Reputation: 11

This issue only occurs in your test environment, install chrome plugin called "Allow-Control-Allow-Origin: *", this solve your problem.

when you emulated the App in iOS or Android all works ok.

Regards;

Upvotes: 1

Related Questions