Laravel Excel giving CORS error only dev server

Issue

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)

I have followed these instructions on local and then on dev server, but I cannot figure out why Im getting this problem only on the dev server. I have even confirmed that php_zip and php_xml are enabled.

I am not getting errors in my logs.

Client side Angular code

getExceptionsReport: function getExceptionsReport() {
    var apiBase = apiUrl + 'exceptions-company-reports';
    var config = {
        responseType: 'blob'
    };
    return $http.get(apiBase, config);
}

Server side:

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 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('Company| Company Report');

            // Chain the setters
            $excel->setCreator('Company')
                  ->setCompany('Company');
            $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');
    }

}

Solution

I came up with a work around of sorts, I guess a little better than a workaround as there is nothing wrong with the code.

I decided to save the file to the server instead and then send that file as a response instead of relying on the extension to do it. Still quite frustrating because I will never really know what the error was. What makes it more frustrating is that I know it can work as it does on my local. I wont mark this answer as correct until in case someone has a better one.

    Excel::create('Filename2', function($excel) use($rows) {
        // other code

    })->save('xlsx');

    return response()->file(storage_path().'/exports/Filename2.xlsx');

I am also deleting the file immediately after with a DELETE request

public function destroy( $id ) {
    \File::Delete(storage_path().'/exports/Filename2.xlsx');
    return response()->json( [ 'success' => 'Report has been removed from server' ], 200 );
}

Answered By – Adrian

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published