我在localhost上运行2个tomcat服务器。一台服务器是lmap服务器,我指向localhost / public / index.php目录以获取我的网站主页。
我有另一个本地服务器在8080后运行。我试图在端口8090上向我的api发出ajax get请求,但是我收到一个错误:
XMLHttpRequest cannot load http://localhost:8090/posts?user-id=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我使用以下php脚本和ajax:
<?php
include ("HeaderPanel.html");
?>
<br/> <br/> <br/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'http://localhost:8090/posts?user-id=1',
crossOrigin: true,
type: 'GET',
success: function(data) {
alert(data);
}
})
});
</script>
<?php
// include ('content.php');
// include ('footer.php');
?>