I fear this may be flagged as a duplicate of my own question but I have honestly exhuasted everything , even from that question...
The problem:
I am making a get request to my Laravel API and getting the following error
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.example.com/exceptions-company-reports. (Reason: CORS header 'Access-Control-Allow-Origin' missing)
Note, this error only appears for this particular request. Not the other ones below. However I did try make a new one and got the same error despite everything else continuing to work on the application
I cannot define what is happening and why that particular request does not work and others do.
Things to note:
- Everyting is working fine on my local vagrant server, replica code
- All other requests are working fine, even to that controller
- The routes are appearing correctly when I run
php artisan route:list
- I have run
composer update
both on local and this server - My error logs are showing no php/syntax errors, but errors are working as they do show up when there actually is an error
- I have stripped all the code below to have bare functions and I am still getting the same problem. this includes removing the likes of
Excel
, stripping other potential problem causing functions. And besides, the lack of PHP errors should indicate that is not the cause anyway. - AngularJS v1.6.2 and Laravel 5.4.16
Client side Angular code (the middle function is the one giving me the CORS error), the other two work perfectly:
getMainEirReports: function getMainEirReports() {
var apiBase = apiUrl + 'main-eir-reports';
var config = {
//responseType: 'blob'
};
return $http.get(apiBase, config);
},
getExceptionsReport: function getExceptionsReport() {
var apiBase = apiUrl + 'exceptions-company-reports';
var config = {
responseType: 'blob'
};
return $http.get(apiBase, config);
},
getExceptionsReportList: function getExceptionsReport() {
var apiBase = apiUrl + 'exceptions-company-reports-list';
var config = { };
return $http.get(apiBase, config);
}
Server Side (routes/web.php)
Route::group([ 'middleware' => 'cors'], function()
{
// Company Reporting
Route::get('main-company-reports', 'CompanyreportingController@mainCompanyReports');
Route::get('exceptions-company-reports', 'CompanyreportingController@exceptionsCompanyReports');
Route::get('exceptions-company-reports-list', 'CompanyreportingController@exceptionsCompanyReportsList');
});
Server side (CompanyreportingController.php):
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\PublishCompanyreportingRequest;
use DB;
use Auth;
use Excel;
class CompanyreportingController extends Controller {
public function __construct() {
$this->middleware( 'jwt.auth' );
$this->middleware( 'role:company-reports' );
}
public function mainCompanyReports( PublishCompanyreportingRequest $requestData ) {
$mainReports = DB::table( 'main_reports' )->select('label_id','tracking_number','unit_status')->get();
return response()->json( $mainReports );
}
public function exceptionsCompanyReports( PublishCompanyreportingRequest $requestData ) {
$list = DB::table( 'exceptions_reports' )->select('created_at','account_number','customer_name','fp','seriel_number','comment','grade','item_number','description')->get();
$rows = array();
foreach($list as $item) {
$rows[] = array(
"Received" => $item->created_at,
"Account Number"=> $item->account_number,
"Customer Name" => $item->customer_name,
"FP"=> $item->fp,
"Serial Number" => $item->seriel_number,
"Comment" => $item->comment,
"Grade" => $item->grade,
"Item Number" => $item->item_number,
"Description" => $item->description,
);
}
Excel::create('Filename2', function($excel) use($rows) {
// Set the title
$excel->setTitle('Iqutech | Company Exceptions Report');
// Chain the setters
$excel->setCreator('Iqutech')
->setCompany('Iqutech');
$excel->sheet('Exceptions Report', function($sheet) use($rows) {
$sheet->fromArray($rows);
$sheet->row(1, function($row) {
// call cell manipulation methods
$row->setBackground('#DDDDDD');
$row->setFontFamily('Calibri');
$row->setFontSize(14);
});
$sheet->setStyle(array(
'font' => array(
'name' => 'Calibri',
'size' => 14
)
));
});
// Call them separately
$excel->setDescription('A demonstration to change the file properties');
})->download('xlsx');
}
public function exceptionsCompanyReportsList( PublishCompanyreportingRequest $requestData ) {
$list = DB::table( 'exceptions_reports' )->select('created_at','account_number','customer_name','fp','seriel_number','comment','grade','item_number','description')->get();
return response()->json( $list );
}
}
Routes:
| | GET|HEAD | exceptions-company-reports | | App\Http\Controllers\CompanyreportingController@exceptionsCompanyReports | web,cors,jwt.auth,role:company-reports |
| | GET|HEAD | exceptions-company-reports-list | | App\Http\Controllers\CompanyreportingController@exceptionsCompanyReportsList | web,cors,jwt.auth,role:company-reports |
| | GET|HEAD | main-company-reports | | App\Http\Controllers\CompanyreportingController@mainCompanyReports | web,cors,jwt.auth,role:company-reports |
via Adrian