0

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.

1 Answer 1

1

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;

Sign up to request clarification or add additional context in comments.

Comments

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.