php - 在本地运行 html 和 php 页面时避免跨源错误
问题描述
我在一个目录中有文件 test.html 和 test.php:
测试.html:
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.6/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class NameForm extends React.Component {
componentDidMount(){
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
console.log(xmlHttp.responseText)
}
}.bind(this)
xmlHttp.open("GET","test.php",true)
xmlHttp.send(null)
event.preventDefault()
}
render() {
return (<div/>)
}
}
ReactDOM.render(
<NameForm />,
document.getElementById('root')
);
</script>
</body>
测试.php
<?php
header("Access-Control-Allow-Origin: http://localhost:1113")
header("Access-Control-Allow-Methods: GET, PUT, POST")
echo "this is a test"
?>
我可以在网站上运行这些,但不能在我的计算机上运行,我希望能够在本地运行它们以进行测试。我试图通过在 php 文件中添加标题来解决这个问题,但这仍然不起作用。我怎样才能解决这个问题?谢谢
解决方案
推荐阅读
- azure-service-fabric - V2Listener 未找到错误
- scala - 在没有构建工具的情况下如何管理 Scala 应用程序?
- html - 当窗口宽度较高时,使视口认为最大宽度为 1200px
- c# - C# .Net 可以通过 Process() 访问 bcdboot 命令
- android - 如何从当前运行的应用程序中获取活动片段的名称?
- java - 如何在用户想要之后自动更改图像按钮?
- android - 如何在Android中制作透明(alpha)渐变蒙版以使一层淡入透明?
- javascript - 努力理解带 Javascript 的 AJAX
- javascript - 有什么方法可以回溯使用javascript中该函数的onclick属性调用函数的按钮
- python - 我如何在 pycryptodome 中使用这个 pycrypto 代码