I am working with Angular Js and Laravel.In angular js I have used route provider to send request from angular js to laravel.
In angular js I have create route js where I include html template using $routeProvider. Here,below is route.js
var app = angular.module("supportApp",['ngRoute']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: '../views/home.html',
controller: 'AdminController'
}).
when('/masters/countries', {
templateUrl: '../views/masters/countries.html'
});
}
]);
Now, I have created masters.js where all masters controller are stored in.Here you can see masters.js
app.controller('CountriesController',function($scope,$http){
var countries = this;
$http.get("http://api.local.support.com/masters/countries")
.then(function mySuccess(responce) {
countries.countrydata = responce["data"]["data"];
});
this.saveAdd = function() {
console.log(countries.countries);
$http.post("http://api.local.support.com/masters/countries/store",countries.countries)
.then(function mySuccess(response) {
console.log(response);
});
}
});
Here, I have create middleware in laravel to accept request from another domain. Cors.php
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN'])
->header('Access-Control-Allow-Methods', 'POST, OPTIONS')
->header('Access-Control-Allow-Credentials', 'true')
->header('Access-Control-Max-Age', '10000')
->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
}
}
My countries.html page is look likes below :
<div ng-controller="CountriesController as country">
<div class="modal fade" id="addNew" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-primary">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="icons-office-52"></i></button>
<h4 class="modal-title"><strong>Add New Country</strong> </h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="field-1" class="control-label">Country</label>
<input type="text" class="form-control" name="name" ng-model="country.countries.name">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="field-1" class="control-label">Sortname</label>
<input type="text" class="form-control" name="alias" ng-model="country.countries.sortname">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-embossed" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary btn-embossed" data-dismiss="modal" ng-click="country.saveAdd()">Save</button>
</div>
</div>
</div>
</div>
</div>
Here, get method in angular js is working perfet. But it gives an error in post method.
Error : XMLHttpRequest cannot load http://api.local.support.com/masters/countries/store. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://local.support.com' is therefore not allowed access.
Note : Here I have created two vertual host http://api.local.support.com
for laravel and http://local.support.com
for angular js and view file.
So, How can I resolve this Error?