gpt4 book ai didi

angularjs - Laravel Excel 仅提供 CORS 错误开发服务器

转载 作者:行者123 更新时间:2023-12-02 17:20:26 24 4
gpt4 key购买 nike

问题:

我正在向我的 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_zipphp_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');
}

}

最佳答案

Laravel-Excel 不会为您添加标题。因此,为了避免 CORS 问题,请添加此 header :

Excel::create('Contactos', function($excel) use ($results) {
...
})->export('xlsx', ['Access-Control-Allow-Origin'=>'*']);

关于angularjs - Laravel Excel 仅提供 CORS 错误开发服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43185207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com