Access to XMLHttpRequest at 'http://localhost:8080/api/login' from origin 'http://localhost:8080'

 

解决方案:

1.在被请求的php文件中加入请求头部

header('Access-Control-Allow-Origin: *');
<?php 
	header('Access-Control-Allow-Origin: *');
	$arr = [
		array('id'=>1,'title'=>'one')
	];

	echo json_encode($arr);
 ?>

 

2.nginx代理配置项加上允许跨域:

 add_header Access-Control-Allow-Origin *;
 add_header Access-Control-Allow-Credentials 'true'; 
 add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
 add_header Access-Control-Allow-Headers *;