【发布时间】:2017-08-30 20:17:09
【问题描述】:
当我尝试从链接(php 服务器)检索对象列表时出现错误。
多源请求(跨域请求)的阻塞:“同 源”策略不允许访问位于 http://localhost/eReport/index.php。原因: CORS 中缺少“访问控制授权来源”令牌 "Access-Control-Allow-Headers" CORS 标头。
我添加了这个链接上推荐的这个 tuto 的标题,但我仍然有这个错误。
你能帮帮我吗?
我的服务页面:
@Injectable()
export class ReportService{
private baseUrl: string = 'http://localhost/report/reports.php';
constructor(private http : Http){}
getAll(): Observable<Report[]>{
let report$ = this.http
.get(`${this.baseUrl}`, { headers: this.getHeaders()})
.map(mapReports);
return report$;
}
private getHeaders(){
// I included these headers because otherwise FireFox
// will request text/html
let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
return headers;
}
get(id: number): Observable<Report> {
let report$ = this.http
.get(`${this.baseUrl}/report/${id}`, {headers: this.getHeaders()})
.map(mapReport);
return report$;
}
我的 php 页面
header("Access-Control-Allow-Origin: *");
$tab = array(
array('id'=> '12', 'name'=> 'test','description' => '2018-04-01','url'=>'../../../assets/img/chat1.png' ),
array('id'=> '13', 'name'=> 'test','description' => '2018-04-01','url'=>'../../../assets/img/highcharts.png' )
);
echo json_encode($tab);
?>
【问题讨论】:
-
你遇到了什么错误?
-
阻止多源请求(跨源请求):“同源”策略不允许访问位于 http://localhost/eReport/index.php 的远程资源。原因:CORS“Access-Control-Allow-Headers”CORS 标头中缺少“Access-control-authorization-origin”令牌。
-
这是来自您的服务器端,您需要在那里允许跨不同域的请求
标签: javascript php angular