Laravel Excel 仅提供 CORS 错误开发服务器
Laravel Excel giving CORS error only dev server
问题:
我正在向我的 Laravel API 发出获取请求并收到以下错误
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)
我在本地和开发服务器上关注了 these instructions,但我不明白为什么我只在开发服务器上遇到这个问题。我什至已经确认 php_zip
和 php_xml
已启用。
我的日志中没有错误。
客户端Angular代码
getExceptionsReport: function getExceptionsReport() {
var apiBase = apiUrl + 'exceptions-company-reports';
var config = {
responseType: 'blob'
};
return $http.get(apiBase, config);
}
服务器端:
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');
}
}
我想出了一个变通办法,我猜比变通办法好一点,因为代码没有任何问题。
我决定改为将文件保存到服务器,然后将该文件作为响应发送,而不是依赖扩展来执行此操作。仍然非常令人沮丧,因为我永远不会真正知道错误是什么。更令人沮丧的是,我知道它可以像在本地一样工作。在万一有人有更好的答案之前,我不会将此答案标记为正确的。
Excel::create('Filename2', function($excel) use($rows) {
// other code
})->save('xlsx');
return response()->file(storage_path().'/exports/Filename2.xlsx');
我也在 DELETE
请求后立即删除文件
public function destroy( $id ) {
\File::Delete(storage_path().'/exports/Filename2.xlsx');
return response()->json( [ 'success' => 'Report has been removed from server' ], 200 );
}
Laravel-Excel 不会为您添加 header。因此,为了避免 CORS 问题,请添加此 header:
Excel::create('Contactos', function($excel) use ($results) {
...
})->export('xlsx', ['Access-Control-Allow-Origin'=>'*']);
在我的例子中,这是因为它没有安装 zip 扩展。
它向我显示跨源请求被阻止,但这不是错误
问题:
我正在向我的 Laravel API 发出获取请求并收到以下错误
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)
我在本地和开发服务器上关注了 these instructions,但我不明白为什么我只在开发服务器上遇到这个问题。我什至已经确认 php_zip
和 php_xml
已启用。
我的日志中没有错误。
客户端Angular代码
getExceptionsReport: function getExceptionsReport() {
var apiBase = apiUrl + 'exceptions-company-reports';
var config = {
responseType: 'blob'
};
return $http.get(apiBase, config);
}
服务器端:
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');
}
}
我想出了一个变通办法,我猜比变通办法好一点,因为代码没有任何问题。
我决定改为将文件保存到服务器,然后将该文件作为响应发送,而不是依赖扩展来执行此操作。仍然非常令人沮丧,因为我永远不会真正知道错误是什么。更令人沮丧的是,我知道它可以像在本地一样工作。在万一有人有更好的答案之前,我不会将此答案标记为正确的。
Excel::create('Filename2', function($excel) use($rows) {
// other code
})->save('xlsx');
return response()->file(storage_path().'/exports/Filename2.xlsx');
我也在 DELETE
请求后立即删除文件
public function destroy( $id ) {
\File::Delete(storage_path().'/exports/Filename2.xlsx');
return response()->json( [ 'success' => 'Report has been removed from server' ], 200 );
}
Laravel-Excel 不会为您添加 header。因此,为了避免 CORS 问题,请添加此 header:
Excel::create('Contactos', function($excel) use ($results) {
...
})->export('xlsx', ['Access-Control-Allow-Origin'=>'*']);
在我的例子中,这是因为它没有安装 zip 扩展。
它向我显示跨源请求被阻止,但这不是错误